I have faced some weird issue, I am sending request with Postman (using raw body) and getting correct response, but When I am making request with go, I am getting different response.
I tried this:
url := "http://example.com"
payload := strings.NewReader("key=value")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("content-type", "application/x-www-form-urlencoded")
req.Header.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36")
req.Header.Add("cache-control", "no-cache")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
fmt.Println(res)
fmt.Println(string(body))
But got wrong response, also tried using values
values := url.Values{}
values.Add("key", "value")
payload := strings.NewReader(values.Encode())
But still getting wrong response, is there a way to simulate postman's raw method in Go?
Postman is automatically adding cookies to my request, just added Cookie header and the problem is fixed
Here is a postman tip: click on the code button/link right below the send button, next to the cookies link and from the popup you can select which language to get code output of the what postman would execute, Go is an option here
For your example I think what you need to do is remove the line that explicitly sets the content-type
to have it match the raw body response in postman