-1

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)         
bhux
  • 43
  • 6

1 Answers1

1

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))))
Alexey Soshin
  • 16,718
  • 2
  • 31
  • 40
  • I did try this before, and just tried it again, but the results are the same. The receiving side data is completely empty. What makes you think I am not sending valid Json? – bhux Sep 10 '16 at 17:26
  • 1
    Some parsers expecting to always receive a Json object as a top level, that's why. Can we see also the receiving side? I'm starting to think that it gets data somewhat differently, even though curl works. – Alexey Soshin Sep 11 '16 at 08:11
  • Alexey - thank you for the help on this -- I have added the receiving side, which is in Python using Flask. – bhux Sep 11 '16 at 09:11
  • 1
    You're using request.form instead of request.data. With curl you don't specify headers, so it falls back to application/x-www-form-urlencoded, but in Go you do: application/json – Alexey Soshin Sep 12 '16 at 17:05
  • 1
    Thank you Alexey! That was indeed the issue - I had believed I had tried that previously, but in this situation you were completely correct and that is where the data was hiding. Much regards, and thank you! – bhux Sep 13 '16 at 00:15