How do I send json via Post method?
I'm trying:
creatorJSON, _ := json.Marshal(creator)
fmt.Println(string(creatorJSON))
body, _, _ := fasthttp.Post(creatorJSON, "http://localhost:3000", creatorJSON)
// Put these in the global scope so they don't get converted for each request.
var strPost = []byte("POST")
var strRequestURI = []byte("http://localhost:3000")
req := fasthttp.AcquireRequest()
req.SetBody(creatorJSON)
req.Header.SetMethodBytes(strPost)
req.SetRequestURIBytes(strRequestURI)
res := fasthttp.AcquireResponse()
if err := fasthttp.Do(req, res); err != nil {
panic("handle error")
}
fasthttp.ReleaseRequest(req)
body := res.Body()
// Do something with body.
fasthttp.ReleaseResponse(res) // Only when you are done with body!
For future visitors, the previous code sample missing
req.Header.SetContentType("application/json")
Most helpful comment
For future visitors, the previous code sample missing