当前位置: 首页 > 知识库问答 >
问题:

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

司马高韵
2023-03-14

我已经尝试了StackOverflow上的每一个例子,接收端总是有一个空的有效负载。

我排除了接收方,因为能够做到这一点:

curl-XPOST'http://supersecreturl/mypost“-d”[{“我向里奇发誓”:“这个json是100%有效的”},{“我甚至可以”:“复制并粘贴到一个curl-POST请求中,并在远程端完美地接收它”}]

求求你帮帮我,我快疯了。。

/// 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个答案

孔礼骞
2023-03-14

我可以看到有两个问题。

  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))))
 类似资料:
  • 我无法将数据从postman发布到我正在使用的mysql数据库express 我尝试过使用Body和Raw x-www-form-urlencoded 这是我的代码(是的,所有内容都在同一个文件中,我知道这不是一件好事,我很抱歉) 这是我在邮递员和数据库中的截图错误

  • 问题内容: 我必须将以下数据发送到URl的Web服务 要发送的数据格式为: 其中,和是键,并具有通过edittext字符串获取的相应值字符串。我在单击按钮时发布此数据。 我没有收到任何回应,因为我试图与我的合作开发者在他的iPhone相同的应用程序iPhone版本中与我的合作开发者进行反检查,因为服务器说用户名和密码无效。 我的课是: 请协助我,我是JSON和Android.Thanx解析的新手。

  • 当我尝试使用以下命令将数据发布到我的REST API(使用Spring DATA JPA REST创建)时: 我得到这个错误:

  • 问题内容: 我在将数据发送到在线数据库时遇到问题。当我检查数据库时,似乎什么都没有发布。我对收到的响应执行了NSLog,它为空。 这是.php: 但是,如果我将$ response硬编码为某个字符串值,而NSLog接收到的响应,它将接收适当的字符串值。 这是我的代码: 是不是无法插入IMEI(这就是为什么它不发布)或其他问题的事实? 谢谢你的协助。 问题答案: 一些观察: 您应该使用interfa

  • 我想从React获取文件上传并将其发送到SpringBoot。我试图从React FormData发布,它将包含文件名和XML文件的键值对。因此,当我尝试将FormData发布到后端(即Springboot)时,它返回: 这是我的Springboot控制器: 我已经尝试在Axios post请求的头中指定multipart/form-data,但似乎不起作用。问题出在我的请求里还是出在我的控制器上

  • 我们的组织有一个远程SQL数据库,我正试图使用PyODBC连接到该数据库。 下面是测试代码:- 但是,当我尝试使用PyODBC连接时,我无法连接并得到以下错误。 PyODBC.OperationalError:('08001','[08001][Microsoft][ODBC SQL Server驱动程序][DBNETLIB]SQL Server不存在或访问被拒绝.(17)(SQLDriverCo