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

Lad*_*vec 210 rest json go apiary

我尝试使用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())

}
Run Code Online (Sandbox Code Playgroud)

此代码不能正确发送JSON,但我不知道为什么.每次调用时JSON字符串都可以不同.我不能用Struct它.

One*_*One 446

我不熟悉午睡,但使用Golang的net/http包很好(游乐场):

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))
}
Run Code Online (Sandbox Code Playgroud)

  • @Altenrion +1用于固定带名称建议. (7认同)
  • @Altenrion它无法在操场上工作,我只是用它来粘贴代码,你无法从它打开外部连接. (5认同)
  • 只是警告,不要忘记默认情况下golang http客户端永远不会超时,因此对于现实世界来说,最好按照`client.Timeout = time.Second * 15`的方式进行设置。 (5认同)
  • 现在操场上出现了恐慌。您可能需要修复或更新某些内容吗? (2认同)

gao*_*idf 89

你可以post用来发布你的json.

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

jsonValue, _ := json.Marshal(values)

resp, err := http.Post(authAuthenticatorUrl, "application/json", bytes.NewBuffer(jsonValue))
Run Code Online (Sandbox Code Playgroud)

  • 我收到此错误:`无法在http.Post的参数中将jsonValue(类型[] byte)用作io.Reader类型:[] byte未实现io.Reader(缺少Read方法)` (3认同)
  • 如果重要的话,我正在使用 1.7。@OneOfOne 列出的代码有效(它也使用 `bytes.NewBuffer()` 但使用 `http.NewRequest` 而不是 `http.Post`) (2认同)
  • 根据 https://golang.org/pkg/net/http/#Post,“调用者在完成读取后应关闭 `resp.Body`。如果提供的正文是 `io.Closer`,则在完成后关闭它的请求。” 作为 Go 新手,我如何判断主体是否是 `io.Closer`,或者换句话说,这个示例是否安全? (2认同)

A-l*_*bby 11

除了标准的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)
    }
}
Run Code Online (Sandbox Code Playgroud)

这取决于您希望如何实现.我创建这个库是因为我遇到了同样的问题,我希望代码更短,易于使用json,并且在我的代码库和生产系统中更易于维护.

  • @ user1513388在任何语言的任何场景中提供跳过TLS验证的代码示例总是一个可怕的想法...你不小心使访问StackOverflow的新手复制/粘贴"变通办法"并且不了解为什么_fixing_的性质TLS错误至关重要.修复证书导入路径(如果使用自签名进行测试,导入这些路径)或修复计算机的证书链,或找出服务器提供无法通过客户端验证的无效证书的原因. (45认同)
  • 我不太喜欢这个答案的一件事是它组合 JSON 对象的方式,这可能通过注入被利用。更好的方法是组合一个对象,然后将其转换为 JSON(使用适当的转义)。 (2认同)

Nin*_*ham 9

如果您已经有一个结构。

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

// .....

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

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

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)
Run Code Online (Sandbox Code Playgroud)


mes*_*kin 7

http 或 https 的 post 请求示例

    //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)
Run Code Online (Sandbox Code Playgroud)


小智 6

如另一个答案中所述,将io.Pipe用于大型请求体。此方法通过将数据从 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.
Run Code Online (Sandbox Code Playgroud)