AlvaroSantisteban
AlvaroSantisteban

Reputation: 5336

Go - http.Post method returns 400 Bad Request while http.Get seems to work

With the following code:

//fpCode and fpParams are strings
ingestionBody := strings.NewReader(fpCode+fpParams)
resp, err := http.Post("http://192.168.1.151:8080/ingest?", "text/plain", ingestionBody)

I'm getting the error message: "HTTP/1.1 POST /ingest" - 400 Bad Request

I don't know if I'm not using the Post method right (even when in this answer, they seem to use it in a similar way. Is the only example that I was able to find, unfortunatelly Go documentation lacks of examples), the problem is with the second parameter, which should be something different (but I also tried "text/*") or there is something important that I'm missing.

Upvotes: 0

Views: 1888

Answers (2)

Joe
Joe

Reputation: 6610

Perhpas you can try http.PostForm:

form := url.Values{}
form.Add("field1", a)
form.Add("field2", b)
http.PostForm("http://192.168.1.151:8080/ingest", form)

Upvotes: 1

Daniel
Daniel

Reputation: 38771

If you're doing a POST you should probably be using a content-type of application/x-www-form-urlencoded or multipart/form-data.

Ultimately you need to look at the server logs to determine why the request is failing.

You might try http.PostForm() instead.

Upvotes: 1

Related Questions