go-resty / resty

Simple HTTP and REST client library for Go
MIT License
10.01k stars 706 forks source link

Is it possible to merge SetResult() with ForceContentType("application/json") into a single method #850

Closed Snax1210 closed 3 weeks ago

Snax1210 commented 1 month ago

The original intention of using SetResult() is to convert the response into a struct, but sometimes the response header is not "application/json", and you need to add ForceContentType(" application/json") separately

jeevatkm commented 1 month ago

I do not understand about

into a single method

But if you're asking whether it can be used together in the request chain, then yes, you can.

Example:

client := resty.New()
resp, err = client.R().
    SetBody(user).
    SetResult(&AuthSuccess{}).
    ForceContentType("application/json").
    Post("https://example.com/login")

fmt.Println(err, resp)
Snax1210 commented 1 month ago

In other words, could we add ForceContentType("application/json") to SetResult(&AuthSuccess{}) so that you don't have to consider the type of response header when mapping to structs

jeevatkm commented 1 month ago

When you set the ForceContentType("application/json"),, then Resty uses this content type value when Unmarshalling the response body, not the response header value.