douguyi3903 2018-10-30 15:15
浏览 54
已采纳

curl请求中未使用的标题

I'm trying to emulate a curl -X GET with Go, but the server I'm contacting has authentication. I've followed several sites that recommend me to use r.Header.Add(), but I can't get my curl call to work.

My curl call that actually returns something: curl -X GET https://myserver.com/test/anothertest -H 'x-access-token: a1b2c3d4'

My code that doesn't return the expected JSON object:

func get(api string, headers map[string]string, dataStruct interface{}) (data interface{}, err error) {

    req, _ := http.NewRequest("GET", api, nil)
    for k, v := range headers {
        req.Header[k] = []string{v}
    }

    currentHeader, _ := httputil.DumpRequestOut(req, true)
    fmt.Println(string(currentHeader))

    client := &http.Client{}
    resp, err := client.Do(req)

    if err != nil {
        return
    }
    defer resp.Body.Close()

    data = dataStruct
    err = getJson(api, &data)
    if err != nil {
        return
    }

    return
}

func main() {
    // args := parse_args(Options{})

    args := Options{Api: "my_server",
    Headers: map[string]string{
        "x-access-token": "a1b2c3d4"}}

    body, _ := get(args.Api, args.Headers, HistoryDecoder{})
    fmt.Println(body)
}

Returns:

GET /v1pre3/users/current HTTP/1.1
Host: my_server
User-Agent: Go-http-client/1.1
X-Access-Token: a1b2c3d4
Accept-Encoding: gzip


map[ResponseStatus:map[Message:Please ensure that valid credentials are being provided for this API call (This request requires authentication but none were provided)] Notifications:[map[Type:error Item:Please ensure that valid credentials are being provided for this API call (This request requires authentication but none were provided)]]]

Could anyone please tell me what I'm doing wrong?

edit adding curl -v response

* About to connect() to my_server port 443 (#0)
*   Trying 54.210.110.53...
* Connected to my_server (54.210.110.53) port 443 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
*   CAfile: /etc/pki/tls/certs/ca-bundle.crt
  CApath: none
* SSL connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
* Server certificate:
*       subject: CN=*.cloud-test.com
*       start date: Feb 28 00:00:00 2018 GMT
*       expire date: Mar 28 12:00:00 2019 GMT
*       common name: *.cloud-test.com
*       issuer: CN=Amazon,OU=Server CA 1B,O=Amazon,C=US
> GET /v1pre3/users/current HTTP/1.1
> User-Agent: curl/7.29.0
> Host: my_server
> Accept: */*
> x-access-token: a1b2c3d4
>
< HTTP/1.1 200 OK
< Cache-Control: no-cache, no-store, must-revalidate
< Content-Type: application/json
< Date: Tue, 30 Oct 2018 15:49:00 GMT
< Expires: 0
< Pragma: no-cache
< Server:
< x-capabilities: audit
< X-Content-Type-Options: nosniff
< X-Request-ID: 2018.10.30.15.49.00.9zHjdeQ-ZEmg2IvzTymnxQ
< transfer-encoding: chunked
< Connection: keep-alive
<
* Connection #0 to host my_server left intact
  • 写回答

1条回答 默认 最新

  • douchongbang6011 2018-10-30 21:11
    关注

    After a lot of rewriting and debugging, I figured out that getJson(api, &data) was also making a http.Get call but without headers. Since that took place after the request we were discussing, it bypassed all testing and printing.

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

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题