how to ignore content type with golang?

huangapple go评论120阅读模式
英文:

how to ignore content type with golang?

问题

有人可以告诉我如何在Go语言中忽略内容类型吗?我可以在Java中使用Jsoup的ignoreContentType方法,但是我找不到在Go语言中可以实现类似功能的方法。希望有人可以告诉我,谢谢。

  1. import (
  2. "fmt"
  3. "net/http"
  4. )
  5. func main() {
  6. resp, err := http.Get("http://example.com/")
  7. if err != nil {
  8. fmt.Println(err)
  9. return
  10. }
  11. defer resp.Body.Close()
  12. body, err := ioutil.ReadAll(resp.Body)
  13. if err != nil {
  14. fmt.Println(err)
  15. return
  16. }
  17. fmt.Println(string(body))
  18. }
英文:

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.

  1. Connection.Response response = Jsoup.connect("http://example.com/")
  2. .ignoreContentType(true)
  3. .execute();
  4. System.out.println(response.body());

答案1

得分: 0

Java中的jsoup的ignoreContentType用于在解析响应时忽略文档的Content-Type

在Go语言中,ResponseWriter默认会将Content-Type设置为将前512个字节的数据传递给DetectContentType函数后的结果。

你可以实现自己的ResponseWriter,在其中将自定义的Content-Type设置为"",以忽略响应数据中可能存在的(可能不正确的)内容类型。

  1. func (writer MyOwnResponseWriter) Write(data []byte) (int, error) {
  2. writer.Header().Set("Content-Type", "")
  3. return len(data), nil
  4. }

JimB在评论中建议简单地设置自己的Content-Type,因为server.go中包含以下内容:

  1. // 如果Header中不包含Content-Type行,
  2. // Write函数会将Content-Type设置为将前512个字节的数据传递给DetectContentType函数后的结果。
英文:

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.

  1. func (writer MyOwnResponseWriter) Write(data []byte) (int, error) {
  2. writer.Header().Set("Content-Type", "")
  3. return len(data), nil
  4. }

JimB suggests in the comments to simply set your own Content-Type, as server.go includes:

  1. //If the Header does not contain a Content-Type line,
  2. // Write adds a Content-Type set to the result of
  3. // passing the initial 512 bytes of written data toDetectContentType.

答案2

得分: 0

我在阅读Jsoup的源代码后解决了我的问题,只需设置响应头部:

  1. resp.Header.Set("Transfer-Encoding", "chunked")
  2. resp.Header.Set("Content-Type", "application/json")

谢谢你们。

英文:

I have solved my problem after read Jsoup's source code, just set the response header

  1. resp.Header.Set("Transfer-Encoding", "chunked")
  2. resp.Header.Set("Content-Type", "application/json")

thanks you guys.

huangapple
  • 本文由 发表于 2016年12月29日 16:16:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/41375662.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定