Home  >  Q&A  >  body text

python - requests提交的json.dumps不能被服务器解析

背景:
现有一看上去是内嵌IE应用程序A,可以做一些查询等操作。

目的:
通过借助Wireshark对应用程A的通信过程分析,借助python的requests的库自行构造一个自己能掌控的程序B

状况:
在使用requests库,加载json格式的POST数据,执行特定请求时,遇到对方服务器的报错。

payload_data={"jsonstr":{
"pagesize":10,
"pageindex":1,
"start":"2017-03-01",
"end":"2017-03-13",
"keyword":"张三",
"status":"0"
}
}

r = requests.post(url, headers = headers_comm, data = json.dumps(payload_data))

Wireshark对程序A请求的侦听:

自行构造请求时的报错信息:

猜测这个请求的构造基本成功了,但对端应用程序解析我POST的数据时不能正常识别。
尝试过给json.dumps加, ensure_ascii=False的参数,但报错依旧。
如果这个猜测正确的话,POST数据应该怎样正确dumps?
如果这个猜测不正确,那么可能是哪里的原因?

巴扎黑巴扎黑2741 days ago715

reply all(2)I'll reply

  • 高洛峰

    高洛峰2017-04-18 10:29:08

    Data does not require json.dump. You only need to use json.dump to include the following jsonstr in data. It should be like this, you can try it

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:29:08

    Response Code It’s 500. It’s because the other party made an error during deserialization.
    According to the exception information, the server should deserialize in the following way.

    C#:

    var jsonObj = (new JavaScriptSerializer()).Deserialize<IDictionary<string, string>>(jsonString);
                

    Your payload_data format is wrong, it is a nested dictionary.

    Change payload_data to:
    Python:

    payload_data={
    "pagesize":10,
    "pageindex":1,
    "start":"2017-03-01",
    "end":"2017-03-13",
    "keyword":"张三",
    "status":"0"
    }
    

    That’s it.

    reply
    0
  • Cancelreply