Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从包含JSON和非JSON数据的python发送POST请求_Python_Json_Post - Fatal编程技术网

从包含JSON和非JSON数据的python发送POST请求

从包含JSON和非JSON数据的python发送POST请求,python,json,post,Python,Json,Post,您将如何发送一个请求(来自python),最终在web页面()的live标头中显示如下内容: 在更好的语法中,POST请求如下所示: URL=http://localhost:8080/rest/marker/ json: {"label":"my label here","folderId":null, "url":"#FF0000","comments":"","position":{"lat":39.2965796259061,"lng":-120.16708374023438}} tid

您将如何发送一个请求(来自python),最终在web页面()的live标头中显示如下内容:

在更好的语法中,POST请求如下所示:

URL=http://localhost:8080/rest/marker/
json: {"label":"my label here","folderId":null, "url":"#FF0000","comments":"","position":{"lat":39.2965796259061,"lng":-120.16708374023438}}
tid: "0V7V"
(请忽略数据值,它们在每个测试中都不同)

我尝试了以下几种变体:

a=requests.post("http://localhost:8080/rest/marker",data="json=%7B%22label%22%3A%22stuff%22%2C%22folderId%22%3Anull%2C%22url%22%3A%22%2300FF00%22%2C%22comments%22%3A%22%22%2C%22position%22%3A%7B%22lat%22%3A39.418%2C%22lng%22%3A-120.2%7D%7D&tid=0V7V")

a=requests.post("http://localhost:8080/rest/marker",json={"label":"stuff","folderId":"null","url":"#FF0000","comments":"","position":{"lat":39.4112,"lng":-120.2},"tid":"0V7V"})

a=requests.post("http://localhost:8080/rest/marker/",json={"label":"stuff","folderId":"null","url":"#FF0000","comments":"","position":{"lat":39.4112,"lng":-120.2}},data={"tid":"0V7V"})
我在回复文本中得到的stacktrace总是以这个开头, 这可能只是表明我做错了:

java.lang.ClassCastException: net.sf.json.JSONNull cannot be cast to
net.sf.json.JSONObject
正确的方法是什么?

试试这个:

import json

payload = {
    "label": "my label here",
    "folderId": None,  # converted to null by json serializer
    "url": "#FF0000",
    "comments":"",
    "position": {
        "lat":39.2965796259061,
        "lng":-120.16708374023438,
    }
}

response = requests.post(
    "http://localhost:8080/rest/marker",
    data={'json': json.dumps(payload), 'tid': '0V7V'}
)
从您问题中的细节来看,服务器似乎希望您发布一个表单,其中包含一个名为
json
的字段,其值为json序列化字符串

当您发送表单数据时,值需要为…但是
请求
将自动为您执行此操作。您只需要传入表单数据的dict,尽管仍然需要将有效负载json序列化为
json
key的值

第一个示例的问题是您已经对有效负载进行了URL编码,因此当您将其传递给
请求时,它将以双重编码结束


在第二个示例中,您告诉
请求
发送一个json序列化的负载,就像原始的POST正文而不是表单数据一样。

在响应文本中得到了不同的消息,现在从状态代码开始:500Exception:
Stacktrace:java.lang.NullPointerException是否有方法查看实际发送的内置POST字符串,语法与live header相同?也许您需要调试java服务器。我不是Javaservlet的作者。live header是在使用创建标记的java应用程序的实际GUI功能时捕获的,并且,这是已知的,并且说明了它的工作原理-因此,我真正需要做的就是复制准确的POST请求。您可以尝试使用代理,例如检查发送的请求的内容。。。我相信您也可以单步执行
post()
调用eg with,查看
请求在发送前准备了什么
import json

payload = {
    "label": "my label here",
    "folderId": None,  # converted to null by json serializer
    "url": "#FF0000",
    "comments":"",
    "position": {
        "lat":39.2965796259061,
        "lng":-120.16708374023438,
    }
}

response = requests.post(
    "http://localhost:8080/rest/marker",
    data={'json': json.dumps(payload), 'tid': '0V7V'}
)