rest - How do I send a JSON string in a POST request in Go -


i tried working apiary , made universal template send json mock server , have code:

package main  import (     "encoding/json"     "fmt"     "github.com/jmcvetta/napping"     "log"     "net/http" )  func main() {     url := "http://restapi3.apiary.io/notes"     fmt.println("url:>", url)      s := napping.session{}     h := &http.header{}     h.set("x-custom-header", "myvalue")     s.header = h      var jsonstr = []byte(` {     "title": "buy cheese , bread breakfast." }`)      var data map[string]json.rawmessage     err := json.unmarshal(jsonstr, &data)     if err != nil {         fmt.println(err)     }      resp, err := s.post(url, &data, nil, nil)     if err != nil {         log.fatal(err)     }     fmt.println("response status:", resp.status())     fmt.println("response headers:", resp.httpresponse().header)     fmt.println("response body:", resp.rawtext())  } 

this code doesn't send json properly, don't know why. json string can different in every call. can't use struct this.

i'm not familiar napping, using golang's net/http package works fine (playground):

func main() {     url := "http://restapi3.apiary.io/notes"     fmt.println("url:>", url)      var jsonstr = []byte(`{"title":"buy cheese , bread breakfast."}`)     req, err := http.newrequest("post", url, bytes.newbuffer(jsonstr))     req.header.set("x-custom-header", "myvalue")     req.header.set("content-type", "application/json")      client := &http.client{}     resp, err := client.do(req)     if err != nil {         panic(err)     }     defer resp.body.close()      fmt.println("response status:", resp.status)     fmt.println("response headers:", resp.header)     body, _ := ioutil.readall(resp.body)     fmt.println("response body:", string(body)) } 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -