Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rest 使用Go向本地主机服务器发出GET http请求时,响应为空_Rest_Http_Go_Docker - Fatal编程技术网

Rest 使用Go向本地主机服务器发出GET http请求时,响应为空

Rest 使用Go向本地主机服务器发出GET http请求时,响应为空,rest,http,go,docker,Rest,Http,Go,Docker,我的本地主机服务器正在运行,它只是docker中的容器进程。我正在尝试实现一个Go客户端来构建REST api的创建、列表、更新和删除功能。当我尝试点击URL时,程序成功退出,但给我一个空响应。我进一步观察到,响应类型为“分块”,内容长度为-1。我是新来的,试图找出可能的原因,或者任何人都能提供解决这个问题的方法。这是我的密码- { } 输出- 地图[] 地图[] 这是服务器的JSON输出- { }有几件事你需要改变才能让它工作 粘贴的JSON无效(请使用复选框检查)。一旦确定输入的是有效的JS

我的本地主机服务器正在运行,它只是docker中的容器进程。我正在尝试实现一个Go客户端来构建REST api的创建、列表、更新和删除功能。当我尝试点击URL时,程序成功退出,但给我一个空响应。我进一步观察到,响应类型为“分块”,内容长度为-1。我是新来的,试图找出可能的原因,或者任何人都能提供解决这个问题的方法。这是我的密码- {

}

输出-

地图[]

地图[]

这是服务器的JSON输出-

{


}

有几件事你需要改变才能让它工作

粘贴的JSON无效(请使用复选框检查)。一旦确定输入的是有效的JSON,go代码需要修改如下:

  • 字段需要用告诉JSON解析器字段名称的选项“修饰”
  • Payload
    结构包含基于您提供的JSON描述的
    数据片
修改后的代码将如下所示:

package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "net/http"
)

type Payload struct {
    Stuff []Data `json:"data"`
}

type Data struct {
    Id              string            `json:"id"`
    Links           Links_container   `json:"links"`
    Actions         Actions_container `json:"actions"`
    AccountID       string            `json:"accountId"`
    AgentID         string            `json:"agentId"`
    AllocationState string            `json:"allocationState"`
    Compute         string            `json:"compute"`
    Created         string            `json:"created"`
}

type Links_container map[string]string
type Actions_container map[string]string

func main() {
    url := "http://localhost:8080/v1/containers"
    res, err := http.Get(url)
    if err != nil {
        fmt.Println(err)
    }
    defer res.Body.Close()
    body, err := ioutil.ReadAll(res.Body)
    if err != nil {
        fmt.Println(err)
    }

    var p Payload

    err = json.Unmarshal(body, &p)
    if err != nil {
        panic(err)
    }

    for _, stuff := range p.Stuff {
        fmt.Println(stuff.AccountID, "\n", stuff.Actions, "\n",
            stuff.AgentID, "\n", stuff.AllocationState, "\n", stuff.Compute,
            "\n", stuff.Created, "\n", stuff.Id, "\n", stuff.Links)
    }
}

这里有

您的代码看起来不错,该服务器的输出是什么?输出结构可能有问题。我刚刚发布了输出。服务器的输出中有一些我不关心的额外内容。您声明的结构类型是否可以忽略这一点,或者这是一个问题?
"type": "collection",
"resourceType": "container",
"links": {
"self": "…/v1/containers",
},
"createTypes": { },
"actions": { },
"data": [ 2 items
{
"id": "1i2",
"type": "container",
"links": { … },
"actions": { … },
"accountId": "1a1",
"agentId": null,
"allocationState": "active",
"compute": null,
"created": "2014-11-13T23:47:08Z",
"createdTS": 1415922428119,
"data": { … },
"description": null,
"domain": null,
"firstRunning": "2014-11-13T23:49:16Z",
"firstRunningTS": 1415922556030,
"hostname": null,
"imageId": "1i1",
"imageUuid": "docker:dockerfile/ghost:latest",
"instanceTriggeredStop": "stop",
"kind": "container",
"memoryMb": 256,
"name": "dockercontainer",
"primaryAssociatedIpAddress": null,
"primaryIpAddress": "0.0.0.0.",
"removeTime": null,
"removed": null,
"requestedHostId": "1h1",
"startOnCreate": true,
"state": "running",
"token": "xyz",
"transitioning": "no",
"transitioningMessage": null,
"transitioningProgress": null,
"userdata": null,
"uuid": "29614f31-4322-4af4-9a55-d9c9395f5cb7",
"validHostIds": null,
"zoneId": "1z1",
"environment": { },
"command": null,
"commandArgs": [ ],
"directory": null,
"user": null,
"publishAllPorts": false,
"privileged": false,
"dockerVolumes": null,
"ports": [ ],
},
{ … },
],
"sortLinks": { … },
"pagination": { … },
"sort": null,
"filters": { … },
"createDefaults": { },
package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "net/http"
)

type Payload struct {
    Stuff []Data `json:"data"`
}

type Data struct {
    Id              string            `json:"id"`
    Links           Links_container   `json:"links"`
    Actions         Actions_container `json:"actions"`
    AccountID       string            `json:"accountId"`
    AgentID         string            `json:"agentId"`
    AllocationState string            `json:"allocationState"`
    Compute         string            `json:"compute"`
    Created         string            `json:"created"`
}

type Links_container map[string]string
type Actions_container map[string]string

func main() {
    url := "http://localhost:8080/v1/containers"
    res, err := http.Get(url)
    if err != nil {
        fmt.Println(err)
    }
    defer res.Body.Close()
    body, err := ioutil.ReadAll(res.Body)
    if err != nil {
        fmt.Println(err)
    }

    var p Payload

    err = json.Unmarshal(body, &p)
    if err != nil {
        panic(err)
    }

    for _, stuff := range p.Stuff {
        fmt.Println(stuff.AccountID, "\n", stuff.Actions, "\n",
            stuff.AgentID, "\n", stuff.AllocationState, "\n", stuff.Compute,
            "\n", stuff.Created, "\n", stuff.Id, "\n", stuff.Links)
    }
}