How can I override content type header of every responses for http.Client?

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

How can I override content type header of every responses for http.Client?

问题

我有一个在Go语言中的http.Client,我希望它在处理响应之前将每个响应的内容类型更新为application/json(即使实际情况可能不是这样)。

我应该覆盖哪个属性?

背景:在第三方API中存在一个错误,实际的内容类型是application/json,但它被设置为其他值(错误的值)。

代码片段:

...
requestURL := fmt.Sprintf("http://localhost:%d", serverPort)
req, err := http.NewRequest(http.MethodGet, requestURL, nil)
if err != nil {
    fmt.Printf("client: could not create request: %s\n", err)
    os.Exit(1)
}

res, err := http.DefaultClient.Do(req)
if err != nil {
    fmt.Printf("client: error making http request: %s\n", err)
    os.Exit(1)
}

fmt.Printf("client: got response!\n")
fmt.Printf("client: status code: %d\n", res.StatusCode)

resBody, err := ioutil.ReadAll(res.Body)
if err != nil {
    fmt.Printf("client: could not read response body: %s\n", err)
    os.Exit(1)
}
fmt.Printf("client: response body: %s\n", resBody)
}
英文:

I've got a http.Client in go and I want it to update every content type for every response to application/json (even though it might not be the case) even before it process the response.

Which attribute shall I override?

Context: the underlying issue there's a bug in the third party API where the real content type is application/json but it's set to the other thing (incorrectly).

Code snippet:

...
	requestURL := fmt.Sprintf("http://localhost:%d", serverPort)
	req, err := http.NewRequest(http.MethodGet, requestURL, nil)
	if err != nil {
		fmt.Printf("client: could not create request: %s\n", err)
		os.Exit(1)
	}

	res, err := http.DefaultClient.Do(req)
	if err != nil {
		fmt.Printf("client: error making http request: %s\n", err)
		os.Exit(1)
	}

	fmt.Printf("client: got response!\n")
	fmt.Printf("client: status code: %d\n", res.StatusCode)

	resBody, err := ioutil.ReadAll(res.Body)
	if err != nil {
		fmt.Printf("client: could not read response body: %s\n", err)
		os.Exit(1)
	}
	fmt.Printf("client: response body: %s\n", resBody)
}

答案1

得分: 2

package main

import (
	"fmt"
	"net/http"
)

type MyRoundTripper struct {
	httprt http.RoundTripper
}

func (rt MyRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
	res, err := rt.httprt.RoundTrip(req)

	if err != nil {
		fmt.Printf("错误: %v", err)
	} else {
		res.Header.Set("Content-Type", "application/json")
	}
	return res, err
}

func main() {

	client := &http.Client{Transport: MyRoundTripper{http.DefaultTransport}}
	resp, err := client.Get("https://example.com")

	if err != nil {
		// 处理错误
	}

	fmt.Printf("%+v\n", resp.Header)
}

以上是一个Go语言的代码示例,它使用自定义的MyRoundTripper类型来修改HTTP请求的行为。在RoundTrip方法中,它首先调用原始的RoundTrip方法来发送请求,然后根据需要修改响应的头部信息。在main函数中,它创建了一个使用MyRoundTripper作为传输层的HTTP客户端,并发送一个GET请求到https://example.com。最后,它打印出响应的头部信息。

英文:
package main

import (
	"fmt"
	"net/http"
)

type MyRoundTripper struct {
	httprt http.RoundTripper
}

func (rt MyRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
	res, err := rt.httprt.RoundTrip(req)

	if err != nil {
		fmt.Printf("Error: %v", err)
	} else {
		res.Header.Set("Content-Type", "application/json")
	}
	return res, err
}

func main() {

	client := &http.Client{Transport: MyRoundTripper{http.DefaultTransport}}
	resp, err := client.Get("https://example.com")

	if err != nil {
		// handle error
	}

	fmt.Printf("%+v\n", resp.Header)
}

huangapple
  • 本文由 发表于 2022年6月29日 03:58:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/72792464.html
匿名

发表评论

匿名网友

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

确定