dongli8862 2014-03-28 04:21
浏览 33

获取正在发送的HTTP请求正文

I am doing a request to a https based API. When entering the request URL in Chrome, everything works fine. When doing the same request using the net/http package in Go, I get an error about missing subdomain:

"error": {
  "type": "ApiUnknown",
  "message": "No api specified (via subdomain)"
}

I assume Chrome and Go somehow sends slightly different http requests, so, I need to know what those differences might be.

In Chrome I can use the Developer tools to see the sent request.

How can I get the HTTP request body being sent from the http package?

I can do the following:

package main

import (
    "net/http"
    "os"
)

func main() {
    urlStr := "https://api-name.subdomain.domain.org/page?param=value"

    //client := &http.Client{}
    req, err := http.NewRequest("GET", urlStr, nil)
    if err != nil {
        panic(err)
    }
    req.Write(os.Stdout) // Will this output be equal to the one being sent to the server?
    //resp, err := client.Do(req)
}

Output:

GET /page?param=value HTTP/1.1
Host: api-name.subdomain.domain.org
User-Agent: Go 1.1 package http

Playground: http://play.golang.org/p/Oku0O1yiBt

But I am not sure whether the Transport adds something before the request is sent, or if this is actually all there is.

  • 写回答

1条回答 默认 最新

  • duanen19871021 2014-03-31 12:59
    关注

    I just looked at the net/http std lib sources. As James Henstridge already answered: a few additional authorization and transport related headers might be added. But this is all strictly adhering to the protocol. So I wouldn't assume that there is anything wrong there.

    Your code at the bottom of your example should really print all relevant information. Now it depends on your specific use case why your request fails.

    Did you specify the correct subdomain (as stated by the error message)?

    Do you have a Content-Type header set? The server might check for an appropriate Content-Type depending on your chosen subdomain.

    These are just a few assumptions I can make at this stage.

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向