Fasthttp: how to send body json using Post

Created on 22 Mar 2019  路  2Comments  路  Source: valyala/fasthttp

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)

Most helpful comment

For future visitors, the previous code sample missing

    req.Header.SetContentType("application/json")

All 2 comments

// 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")
Was this page helpful?
0 / 5 - 0 ratings

Related issues

iowelinux picture iowelinux  路  3Comments

vearutop picture vearutop  路  3Comments

Fenny picture Fenny  路  3Comments

horgh picture horgh  路  5Comments

djannot picture djannot  路  3Comments