ChatGPT解决这个技术问题 Extra ChatGPT

如何在 Go 的 POST 请求中发送 JSON 字符串

我尝试使用 Apiary 并制作了一个通用模板来将 JSON 发送到模拟服务器并具有以下代码:

package main

import (
    "encoding/json"
    "fmt"
    "github.com/jmcvetta/napping"
    "log"
    "net/http"
)

func main() {
    url := "http://restapi3.apiary.io/notes"
    fmt.Println("URL:>", url)

    s := napping.Session{}
    h := &http.Header{}
    h.Set("X-Custom-Header", "myvalue")
    s.Header = h

    var jsonStr = []byte(`
{
    "title": "Buy cheese and bread for breakfast."
}`)

    var data map[string]json.RawMessage
    err := json.Unmarshal(jsonStr, &data)
    if err != nil {
        fmt.Println(err)
    }

    resp, err := s.Post(url, &data, nil, nil)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("response Status:", resp.Status())
    fmt.Println("response Headers:", resp.HttpResponse().Header)
    fmt.Println("response Body:", resp.RawText())

}

这段代码没有正确发送 JSON,但我不知道为什么。 JSON 字符串在每次调用中都可能不同。我不能为此使用 Struct

我不熟悉您使用的一些库,但据我了解,您正在尝试发送 Jsons 的地图。为什么不直接发送带有 json 的字符串?
如果要发送 json,为什么要解组 json?
一个小提示,您可以将消息创建为结构或 map[string]interface{} 以添加所需的所有值,然后使用 json.Marshall 将映射或结构转换为 json。
@topo,我研究了小睡的源代码,如果设置了有效负载,他们会调用 json.Marshall ,我不确定为什么它对他不起作用。

O
OneOfOne

我不熟悉打盹,但使用 Golang 的 net/http 包可以正常工作 (playground):

func main() {
    url := "http://restapi3.apiary.io/notes"
    fmt.Println("URL:>", url)

    var jsonStr = []byte(`{"title":"Buy cheese and bread for breakfast."}`)
    req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
    req.Header.Set("X-Custom-Header", "myvalue")
    req.Header.Set("Content-Type", "application/json")

    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()

    fmt.Println("response Status:", resp.Status)
    fmt.Println("response Headers:", resp.Header)
    body, _ := ioutil.ReadAll(resp.Body)
    fmt.Println("response Body:", string(body))
}

现在它在操场上有恐慌。你可能会修复或更新一些东西吗?
@Altenrion 它不能在操场上工作,我只是用它来粘贴代码,你不能从它打开外部连接。
@Altenrion +1 提供可靠的乐队名称建议。
只是一个警告,不要忘记默认情况下 golang http 客户端永远不会超时,所以对于现实世界,最好按照 client.Timeout = time.Second * 15
这可以更新以收集/检查所有错误吗?这是(至少对我而言)谷歌在 Go 中发出帖子请求的最高结果,这是一个很好的答案,但我看到大量示例代码只是忽略了错误,我认为它鼓励了新手的不良做法。再说一次,如果有人经常忽略错误,我想他们最终会明白为什么不这样做,但为什么不鼓励良好的实践呢?
S
Sjon

您可以使用 post 发布您的 json。

values := map[string]string{"username": username, "password": password}

jsonValue, _ := json.Marshal(values)

resp, err := http.Post(authAuthenticatorUrl, "application/json", bytes.NewBuffer(jsonValue))

我收到此错误:cannot use jsonValue (type []byte) as type io.Reader in argument to http.Post: []byte does not implement io.Reader (missing Read method)
@MandarVaze 我认为您可能会弄错 io.Readerhttp.Post,并且 bytes.NewBuffer() 在我的代码中运行良好
我在去 1.7,如果重要的话。 @OneOfOne 列出的代码有效(也使用 bytes.NewBuffer() 但使用 http.NewRequest 而不是 http.Post
根据 golang.org/pkg/net/http/#Post,“调用者在读取完 resp.Body 后应关闭它。如果提供的正文是 io.Closer,则在请求后将其关闭。”作为 Go 新手,我如何判断 body 是否为 io.Closer,或者换句话说,此示例是否安全?
这种方法的唯一限制是您不能设置自定义标题:/
N
Ninh Pham

如果你已经有一个结构。

import (
    "bytes"
    "encoding/json"
    "io"
    "net/http"
    "os"
)

// .....

type Student struct {
    Name    string `json:"name"`
    Address string `json:"address"`
}

// .....

body := &Student{
    Name:    "abc",
    Address: "xyz",
}

payloadBuf := new(bytes.Buffer)
json.NewEncoder(payloadBuf).Encode(body)
req, _ := http.NewRequest("POST", url, payloadBuf)

client := &http.Client{}
res, e := client.Do(req)
if e != nil {
    return e
}

defer res.Body.Close()

fmt.Println("response Status:", res.Status)
// Print the body to the stdout
io.Copy(os.Stdout, res.Body)

完整的 gist


A
A-letubby

除了标准的 net/http 包之外,您还可以考虑使用我的 GoRequest,它包含 net/http,让您的生活更轻松,而无需过多考虑 json 或 struct。但是您也可以在一个请求中混合和匹配它们! (您可以在 gorequest github 页面中查看有关它的更多详细信息)

因此,最终您的代码将如下所示:

func main() {
    url := "http://restapi3.apiary.io/notes"
    fmt.Println("URL:>", url)
    request := gorequest.New()
    titleList := []string{"title1", "title2", "title3"}
    for _, title := range titleList {
        resp, body, errs := request.Post(url).
            Set("X-Custom-Header", "myvalue").
            Send(`{"title":"` + title + `"}`).
            End()
        if errs != nil {
            fmt.Println(errs)
            os.Exit(1)
        }
        fmt.Println("response Status:", resp.Status)
        fmt.Println("response Headers:", resp.Header)
        fmt.Println("response Body:", body)
    }
}

这取决于你想如何实现。我制作这个库是因为我和你有同样的问题,我想要更短的代码,易于与 json 一起使用,并且在我的代码库和生产系统中更易于维护。


如果 GoRequest 包装了 net/http.是否可以添加它来禁用 TLS 的不安全证书? tr := &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, }
@ user1513388 在任何情况下以任何语言提供跳过 TLS 验证的代码示例总是一个糟糕的主意......你不小心让访问 StackOverflow 并且不理解为什么要修复的新手的大量复制/粘贴“变通办法”永久存在TLS 错误至关重要。修复您的证书导入路径(如果使用自签名进行测试,请导入这些路径)或修复您机器的证书链,或者找出您的服务器为何提供无法由您的客户端验证的无效证书。
我不完全喜欢这个答案的一件事是它构成 JSON 对象的方式,它可能通过注入来利用。更好的方法是组合一个对象,然后将其转换为 JSON(使用适当的转义)。
@JohnWhite 我同意,感觉很 ruby/js/pythonic
3
3 revs user13631587

io.Pipe 用于 another answer 中提到的大型请求正文。这种方法通过将数据从 JSON 编码器流式传输到网络来避免在内存中构建整个请求正文。

这个答案建立在另一个答案的基础上,展示了如何处理错误。始终处理错误!

使用管道的 CloseWithError 函数将编码错误传播回从 http.Post 返回的错误。

处理 http.Post 返回的错误

关闭响应正文。

这是代码:

r, w := io.Pipe()

go func() {
    w.CloseWithError(json.NewEncoder(w).Encode(data))
}()

// Ensure that read side of pipe is closed. This
// unblocks goroutine in scenario where http.Post
// errors out before reading the entire request body.
defer r.Close()

resp, err := http.Post(url, r)
if err != nil {
    // Adjust error handling here to meet application requrirements.
    log.Fatal(err)
}
defer resp.Body.Close()
// Use the response here.

m
mesutpiskin

http 或 https 的示例发布请求

    //Encode the data
       postBody, _ := json.Marshal(map[string]string{
          "name":  "Test",
          "email": "Test@Test.com",
       })
       responseBody := bytes.NewBuffer(postBody)
    //Leverage Go's HTTP Post function to make request
       resp, err := http.Post("https://postman-echo.com/post", "application/json", responseBody)
    //Handle Error
       if err != nil {
          log.Fatalf("An Error Occured %v", err)
       }
       defer resp.Body.Close()
    //Read the response body
       body, err := ioutil.ReadAll(resp.Body)
       if err != nil {
          log.Fatalln(err)
       }
       sb := string(body)
       log.Printf(sb)

Z
Zombo

如果要发送大量数据,可以使用管道:

package main

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

func main() {
   m := map[string]int{"SNG_ID": 75498415}
   r, w := io.Pipe()
   go func() {
      json.NewEncoder(w).Encode(m)
      w.Close()
   }()
   http.Post("https://stackoverflow.com", "application/json", r)
}

https://golang.org/pkg/io#Pipe


H
Highdeger

如果你想这样做,你需要使用这个映射来解组 json 字符串。

var data map[string]interface{}

但是如果您每次都需要更改 json 并使您的请求体的初始化更方便,您可以使用此映射来创建 json 体。

var bodyJsonMap map[string]interface{}{
    "key1": val1,
    "key2": val2,
    ...
}

然后将其编组为 json 字符串。