bhux bhux - 2 months ago 13
JSON Question

Unable to successfully POST valid JSON data to a remote url from App Engine using Golang

UPDATE: Please see Alexey's comments below for the solution






I am trying what I thought would be a trivial function to take some valid Json data and post it to a remote url



I've tried every example I could find close to this on StackOverflow, and the receiving side always has an empty payload.

I'm ruling out the receiving side, due to being able to do this:

curl -XPOST 'http://supersecreturl/mypost' -d '[{"i sware to ritchie":"this json is 100 percent valid"},{"i can even":"copy and paste it into a curl POST request and receive it flawlessly on the remote side"}]'


Please help, I'm loosing my mind here..




/// Here is approximately my code - had to remove the valid url and the JSON content



func PutArticlesJSON(c appengine.Context, articles []*Articlez) (*http.Response){

url := "http://mysecreturl/mypost"
client := urlfetch.Client(c)
jsonarts, _ := json.Marshal(articles)
c.Debugf(" --- What do we have - %v", string(jsonarts)) /// the appengine log shows exactly valid json at this point, such as:
/*
[{"i sware to ritchie":"this json is 100 percent valid"},{"i can even":"copy and paste it into a curl POST request and receive it flawless on the remote side"}]
*/

// tried this way too....
//req, err := http.NewRequest("POST", url, strings.NewReader(string(jsonarts)))
//
req, err := http.NewRequest("POST", url, bytes.NewBuffer(string(jsonStr))) /// on the receiving side, the payload is completely empty no matter what I try
req.Header.Set("Content-Type", "application/json")

resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()


body, _ := ioutil.ReadAll(resp.Body)

return resp
}


#

#!/usr/bin/env python
#
from flask import Flask
from flask import request
import urllib
import json


app = Flask(__name__)

@app.route('/mypost', methods = ['GET','POST'])
def esput():
datapack = request.form
datastream = request.stream
with open("/tmp/log", "a") as myf:
myf.write(str(datastream))
myf.write(str(datapack))
myf.write("\n")
return "all good"


if __name__ == '__main__':
app.run(threaded=True,host='0.0.0.0',port='333',debug=False)

Answer

There are two problems I can see there.

  1. Although you think you're sending a valid Json, you aren't.
  2. NewBuffer should receive []byte, not string

Try it like this:

s := `[{"i sware to ritchie":"this json is 100 percent valid"},{"i can even":"copy and paste it into a curl POST request and receive it flawless on the remote side"}]`

req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(fmt.Sprintf(`{"data":%s}`, s))))
Comments