can somebody tell me how to ignore content type with go? I can invoke ignoreContentType method in Jsoup with java, but i can't find any method in go can do like that. Hope someone will tell me, thanks.
Connection.Response response = Jsoup.connect("http://example.com/")
.ignoreContentType(true)
.execute();
System.out.println(response.body());
I have solved my problem after read Jsoup's source code, just set the response header
resp.Header.Set("Transfer-Encoding", "chunked")
resp.Header.Set("Content-Type", "application/json")
thanks you guys.
Java jsoup ignoreContentType
is there to ignore the document's Content-Type
when parsing the response.
The ResponseWriter
in Go will by default adds a Content-Type set to the result of passing the initial 512 bytes of written data to DetectContentType
.
You can implement your own ResponseWriter
where you set your own Content-Type
to ""
in order to ignore any (possibly incorrect) content type deduced from the Response's data.
func (writer MyOwnResponseWriter) Write(data []byte) (int, error) {
writer.Header().Set("Content-Type", "")
return len(data), nil
}
JimB suggests in the comments to simply set your own Content-Type, as server.go
includes:
//If the Header does not contain a Content-Type line,
// Write adds a Content-Type set to the result of
// passing the initial 512 bytes of written data toDetectContentType.