通过 python 提交 http post 并在 json posted 中看到转义字符
Submitting a http post via python and seeing escape characters in the json posted
我通过 python 发帖(对我来说是全新的语言,所以我确信我忽略了一些基本的东西)并在发到服务器的 json 中看到转义字符,这显然导致无效json。这是我的代码:
import requests
#try three different ways to escape json - all result in the backslash being submitted to the server in the post
json = """{"testId": "616fdb5e-40c1-326a-81a4-433051627e6d","testName": "nameHere"}"""
#json = '{"testId": "616fdb5e-40c1-326a-81a4-433051627e6d","testName": "nameHere"}'
#json = "{\"testId\": \"616fdb5e-40c1-326a-81a4-433051627e6d\",\"testName\": \"nameHere\"}"
response = requests.post("http://localhost:8888", data=None, json=json)
我在本地发布到 fiddler,发现转义字符仍然存在。这是发布的内容:
"{\"testId\": \"616fdb5e-40c1-326a-81a4-433051627e6d\",\"testName\": \"nameHere\"}"
我希望图书馆去掉转义字符。不是这样吗?
另一件奇怪的事情是,当我 运行 代码时,字符不存在,至少从我所知:
json
'{"testId": "616fdb5e-40c1-326a-81a4-433051627e6d","testName": "nameHere"}'
json.find("\")
-1
简短的回答是不要提交字符串,响应方法需要一个字典。有效代码:
import requests
json_dict = {"testId": "616fdb5e-40c1-326a-81a4-433051627e6d","testName": "nameHere"}
response = requests.post("http://localhost:8888", data=None, json=json_dict)
我通过 python 发帖(对我来说是全新的语言,所以我确信我忽略了一些基本的东西)并在发到服务器的 json 中看到转义字符,这显然导致无效json。这是我的代码:
import requests
#try three different ways to escape json - all result in the backslash being submitted to the server in the post
json = """{"testId": "616fdb5e-40c1-326a-81a4-433051627e6d","testName": "nameHere"}"""
#json = '{"testId": "616fdb5e-40c1-326a-81a4-433051627e6d","testName": "nameHere"}'
#json = "{\"testId\": \"616fdb5e-40c1-326a-81a4-433051627e6d\",\"testName\": \"nameHere\"}"
response = requests.post("http://localhost:8888", data=None, json=json)
我在本地发布到 fiddler,发现转义字符仍然存在。这是发布的内容:
"{\"testId\": \"616fdb5e-40c1-326a-81a4-433051627e6d\",\"testName\": \"nameHere\"}"
我希望图书馆去掉转义字符。不是这样吗?
另一件奇怪的事情是,当我 运行 代码时,字符不存在,至少从我所知:
json
'{"testId": "616fdb5e-40c1-326a-81a4-433051627e6d","testName": "nameHere"}'
json.find("\")
-1
简短的回答是不要提交字符串,响应方法需要一个字典。有效代码:
import requests
json_dict = {"testId": "616fdb5e-40c1-326a-81a4-433051627e6d","testName": "nameHere"}
response = requests.post("http://localhost:8888", data=None, json=json_dict)