1

I am trying to call an API so the thing is when I am trying to call it from Postman with "Follow original HTTP method" on it's working fine but on disabling it giving a 400-Bad request response. enter image description here

The same thing I am trying to do in my Go code with http client but not able to get success response. Have tried with CheckRedirect no luck there.

httpClient := http.Client{}
req, err := http.NewRequest(http.MethodPost, "url", bytes.NewBuffer(data))
if err != nil {
    return "", err
}

req.Header.Set("content-type", "multipart/form-data")
resp, err := httpClient.Do(req)
if err != nil {
    return "", err
}

What I can do here to get the success response on redirect? TIA

aakash singh
  • 267
  • 5
  • 19

1 Answers1

0

To not follow any redirects you could use something like:

httpClient := &http.Client{
    CheckRedirect: func(req *http.Request, via []*http.Request) error {
    return http.ErrUseLastResponse
    },
}
req, err := http.NewRequest(http.MethodPost, "url", bytes.NewBuffer(data))
if err != nil {
    return "", err
}

req.Header.Set("content-type", "multipart/form-data")
resp, err := httpClient.Do(req)
if err != nil {
    return "", err
}
DiniFarb
  • 206
  • 2
  • 6
  • I want to follow the redirects – aakash singh Nov 11 '22 at 16:56
  • Ups sorry, then you should have to do nothing special at all. Maybe check your go version cos there was a problem with redirects before go 1.6 => https://github.com/golang/go/issues/12705 – DiniFarb Nov 11 '22 at 17:34