V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
javashell
V2EX  ›  Go 编程语言

Golang http 请求提示重复的 header 头如何解决?

  •  
  •   javashell · 2022-12-03 18:20:29 +08:00 · 1910 次点击
    这是一个创建于 502 天前的主题,其中的信息可能已经有所发展或是发生改变。

    使用 go 自带的 net/http 中的方法请求 url ,总提示too many transfer encodings: ["chunked" "chunked"]导致报错无法获取 Body 内容。 根据关键词找到了相关的issues 在 Golang 的 1.15 版本上才出现的问题,虽说也提供了相关的解决方法(修改 go 的库文件)但是总感觉不方便。 除了 issues 中提到的方法,还有其他方法进行修复吗?

    附上返回包的 header

    HTTP/1.1 200 OK
    transfer-encoding: chunked
    X-Frame-Options: SAMEORIGIN
    Content-Type: application/json;charset=UTF-8
    Transfer-Encoding: chunked
    Date: Thu, 10 Nov 2022 02:23:46 GMT
    
    8 条回复    2022-12-04 12:13:55 +08:00
    Mitt
        1
    Mitt  
       2022-12-03 18:57:59 +08:00 via iPhone
    目测是 map 区分大小写导致的,但还得看你怎么调用的,应该给出你调用的例子
    javashell
        2
    javashell  
    OP
       2022-12-03 19:31:57 +08:00
    @Mitt
    代码是这样的
    ```go
    func login() (Token string, Account string, FullName string) {
    client = &http.Client{}
    data := "提交的数据"
    req, _ := http.NewRequest("POST", "http:///xxxxxx", bytes.NewReader([]byte(data)))
    req.Header.Set("Content-Type", "application/json")
    req.Header.Set("Accept", "*/*")
    req.Header.Set("Cache-Control", "no-cache")

    resp, _ := client.Do(req)

    r, _ := io.ReadAll(resp.Body)

    ...
    ```
    novolunt
        3
    novolunt  
       2022-12-03 22:46:23 +08:00
    以下是人工智障的回答
    Vintingb
        4
    Vintingb  
       2022-12-03 23:10:42 +08:00 via iPhone
    @novolunt 这是啥网站
    novolunt
        5
    novolunt  
       2022-12-03 23:19:42 +08:00
    kingcanfish
        6
    kingcanfish  
       2022-12-04 10:30:47 +08:00
    加个这个请求头试试?
    ```
    req.Header.Set ("Accept-Encoding","identity")
    ```
    alsas
        7
    alsas  
       2022-12-04 10:38:32 +08:00
    @novolunt 禁止中国号码注册 咋办
    javashell
        8
    javashell  
    OP
       2022-12-04 12:13:55 +08:00
    @kingcanfish 这个试过了 禁止分片传输没用
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5309 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 1029ms · UTC 08:37 · PVG 16:37 · LAX 01:37 · JFK 04:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.