gpt4 book ai didi

http - curl 请求中未使用的 header

转载 作者:数据小太阳 更新时间:2023-10-29 03:21:29 25 4
gpt4 key购买 nike

我正在尝试使用 Go 模拟 curl -X GET,但我正在联系的服务器具有身份验证。我关注了几个推荐我使用 r.Header.Add() 的网站,但我无法让 curl 调用正常工作。

我的 curl 调用实际上返回了一些东西:curl -X GET https://myserver.com/test/anothertest -H 'x-access-token: a1b2c3d4'

我的代码没有返回预期的 JSON 对象:

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)
}

返回:

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)]]]

谁能告诉我我做错了什么?

编辑 添加curl -v 响应

* 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

最佳答案

经过大量重写和调试后,我发现 getJson(api, &data) 进行了 http.Get 调用,但没有 header 。由于那是在我们讨论的请求之后发生的,因此它绕过了所有测试和打印。

关于http - curl 请求中未使用的 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53067518/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com