无法使用Golang从应用引擎成功POST有效的JSON数据到远程url



更新:关于解决方案,请参阅下面Alexey的评论


我正在尝试什么,我认为这将是一个微不足道的功能采取一些有效的Json数据,并将其发布到远程url

我已经尝试了每一个例子,我可以找到接近这个在StackOverflow上,接收端总是有一个空的有效载荷。

我排除了接收端,因为可以这样做:

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"}]'

请帮帮我,我都快疯了


/// 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)         

这里有两个问题。

  1. 虽然你认为你正在发送一个有效的Json,你不是。
  2. NewBuffer应该接收[]字节,而不是字符串

试着这样写:

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))))

最新更新