duanduanxi9441 2017-10-10 06:38
浏览 299
已采纳

将CURL转换为Golang代码

I'm trying to execute this exact CURL command in Golang:

curl -X GET -H "Content-Type: application/json" -H "WEB2PY-USER-TOKEN: token-string" -d '{"rfrID": "111111"}' 'url-string'

It's a little weird because it's a GET HTTP call with a JSON body. Our vendor however requires us to call the API like this though. I can't figure out how to make our Golang program execute this API.

This is the Go code that I've tried so far:

data := Payload{
    // fill struct
    RfrID: "111111",
}
payloadBytes, err := json.Marshal(data)
if err != nil {
    // handle err
}
body := bytes.NewReader(payloadBytes)

log.Printf("[INFO] Input body status: %v
", body)

req, err := http.NewRequest("GET", "url-string", body)
if err != nil {
    // handle err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Web2py-User-Token", "token-string")

resp, err := http.DefaultClient.Do(req)
if err != nil {
    // handle err
}

log.Printf("[INFO] Artemis status: %v
", resp)

defer resp.Body.Close()

In my logs the following gets printed:

2017/10/10 07:31:56 [INFO] Input body status: &{[123 34 114 102 114 73 68 34 58 34 49 49 49 49 49 49 34 125] 0 -1}

I expect this body to be a JSON body like:

{"rfrID": "111111"}
  • 写回答

1条回答 默认 最新

  • drwj4061 2017-10-10 07:59
    关注

    Your body is what you think it is, it is just printed in an unexpected format to you.

    bytes.NewReader() returns a pointer to the struct type bytes.Reader. When you print it using log.Printf(), it uses the formatting rules defined in the package doc of fmt, that is, pointers to struct are printed as &{} and values of fields enumerated inside of it. This is what you see. bytes.Reader stores the data it provides in a byte slice:

    [123 34 114 102 114 73 68 34 58 34 49 49 49 49 49 49 34 125]
    

    And this byte slice is equivalent to the JSON string you expect. To verify:

    data := []byte{123, 34, 114, 102, 114, 73, 68, 34, 58, 34, 49, 49, 49, 49, 49, 49, 34, 125}
    fmt.Println(string(data))
    

    Output:

    {"rfrID":"111111"}
    

    Reading from this reader will provide this exact string, as you can see in this example:

    r := bytes.NewReader(data)
    fmt.Println(r)
    readData, err := ioutil.ReadAll(r)
    if err != nil {
        panic(r)
    }
    fmt.Println(string(readData))
    

    Output:

    &{[123 34 114 102 114 73 68 34 58 34 49 49 49 49 49 49 34 125] 0 -1}
    {"rfrID":"111111"}
    

    Try these examples on the Go Playground.

    Differences

    Let's examine the differences between the CURL command and the client you wrote, with this little app which will be the target:

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        r.ParseForm()
        log.Println("Method:", r.Method)
        log.Println("Headers:", r.Header)
        log.Println("Form:", r.Form)
        data, err := ioutil.ReadAll(r.Body)
        log.Println("Body:", data, err)
        log.Println("Body string:", string(data))
    })
    panic(http.ListenAndServe(":8080", nil))
    

    CURL:

    2017/10/10 10:17:26 Method: GET
    2017/10/10 10:17:26 Headers: map[Content-Length:[19] User-Agent:[curl/7.47.0] Accept:[*/*] Content-Type:[application/json] Web2py-User-Token:[token-string]]
    2017/10/10 10:17:26 Form: map[]
    2017/10/10 10:17:26 Body: [123 34 114 102 114 73 68 34 58 32 34 49 49 49 49 49 49 34 125] <nil>
    2017/10/10 10:17:26 Body string: {"rfrID": "111111"}
    

    Go client:

    2017/10/10 10:17:20 Method: GET
    2017/10/10 10:17:20 Headers: map[User-Agent:[Go-http-client/1.1] Content-Length:[18] Content-Type:[application/json] Web2py-User-Token:[token-string] Accept-Encoding:[gzip]]
    2017/10/10 10:17:20 Form: map[]
    2017/10/10 10:17:20 Body: [123 34 114 102 114 73 68 34 58 34 49 49 49 49 49 49 34 125] <nil>
    2017/10/10 10:17:20 Body string: {"rfrID":"111111"}
    

    Despite some headers added automatically by the clients, and a space inside the JSON string (which has no effect just formatting), they are identical.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料