douaoli5328 2016-09-10 03:00
浏览 16
已采纳

无法使用Golang从App Engine成功地将有效JSON数据发布到远程网址

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("
")                                                                                                                                                                                                                              
    return "all good"                                                                                                                              


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

1条回答 默认 最新

  • douxian8883 2016-09-10 10:51
    关注

    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))))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line