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.
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.