删掉丶关于n1 发表于 2019-10-12 17:38

如何利用python的优势巧妙的组装POST-data提交的数据?

上一条返回来的内容:[{"statusCode":200,"type":"rpc","tid":2,"ref":false,"action":"ICS_WR_Warranty_Create_Agent_Controller","method":"calPMFTSystem","result":{"BoxTrayIndicator":"Boxed","Error":"false","Last_Ship_To_Date":"2017-05-25 00:00:00","ProductFrequencyData":[{"FreqeuncyCount":1,"FrequencyType":"ATPO"}],"ShipmentInfo":[{"AGIDDate":"2017-05-25T01:42:11-07:00","Batch":"L709C055","BoxTrayIndicator":"BOX","ChildBoxId":"804327398","Level4Prod":"BX80677I77700K","MaterialMasterNumber":"953655","ShipToCustomerCountryCode":"NL","ShipToCustomerCountryName":"Netherlands","ShipToCustomerGeo":"EMEA","VisualId":"U7NH442803139"}],"ValidationResultCodes":[{"Remarks":" ","ValidationResult_Code":"PMFT-028","ValidationResultCodeId":28},{"Remarks":"","ValidationResult_Code":"PMFT-027","ValidationResultCodeId":27},{"Remarks":"","ValidationResult_Code":"PMFT-035","ValidationResultCodeId":35},{"Remarks":" ","ValidationResult_Code":"PMFT-015","ValidationResultCodeId":15},{"Remarks":"","ValidationResult_Code":"PMFT-034","ValidationResultCodeId":34}]}}]

下一条准备提交的内容:
{"action":"ICS_WR_Warranty_Create_Agent_Controller","method":"doWarrantyCheckProcessor","data":["{\"BoxTrayIndicator\":\"Boxed\",\"Error\":\"false\",\"Last_Ship_To_Date\":\"2017-05-25 00:00:00\",\"ProductFrequencyData\":[{\"FreqeuncyCount\":1,\"FrequencyType\":\"ATPO\"}],\"ShipmentInfo\":[{\"AGIDDate\":\"2017-05-25T01:42:11-07:00\",\"Batch\":\"L709C055\",\"BoxTrayIndicator\":\"BOX\",\"ChildBoxId\":\"804327398\",\"Level4Prod\":\"BX80677I77700K\",\"MaterialMasterNumber\":\"953655\",\"ShipToCustomerCountryCode\":\"NL\",\"ShipToCustomerCountryName\":\"Netherlands\",\"ShipToCustomerGeo\":\"EMEA\",\"VisualId\":\"U7NH442803139\"}],\"ValidationResultCodes\":[{\"Remarks\":\" \",\"ValidationResult_Code\":\"PMFT-028\",\"ValidationResultCodeId\":28},{\"Remarks\":\"\",\"ValidationResult_Code\":\"PMFT-027\",\"ValidationResultCodeId\":27},{\"Remarks\":\"\",\"ValidationResult_Code\":\"PMFT-035\",\"ValidationResultCodeId\":35},{\"Remarks\":\" \",\"ValidationResult_Code\":\"PMFT-015\",\"ValidationResultCodeId\":15},{\"Remarks\":\"\",\"ValidationResult_Code\":\"PMFT-034\",\"ValidationResultCodeId\":34}]}","{\"validateMainLineItem\":1000,\"validateProductId\":\"01t0P0000026L2QQAU\",\"validateProductType\":\"Processor\",\"strFpoNumber\":\"L709C055\",\"strAtpoNumber\":\"U7NH442803139\",\"mmid\":\"953655\"}","953655","BX80677I77700K","","",false],"type":"rpc","tid":3,"ctx":{"csrf":"VmpFPSxNakF4T1MweE1DMHhOVlF3T1Rvd05Eb3dPQzQwTVRaYSw3VTNjM0NiYmJ4ellWSXBEeTRPbGhOLE9UZzBOV1Zt","vid":"0660P000000yK7w","ns":"","ver":38}}

如何利用python的优势巧妙的组装成一个POST-DATA提交的数据?利用的是requests库
目前遇到的问题就是:提交的时候data参数里的引号需要带\"我用json组装完之后 就没有 \"导致提交失败以上是完整提交的内容!

ghoob321 发表于 2019-10-12 17:45

占个沙发。这个没学过。

wuwuxin123 发表于 2019-10-12 17:46

这个问题很尴尬,难以回答

绝版durex 发表于 2019-10-12 17:52

没记错的话 requests.post 中的data参数可以直接传入Python的字典 请求时会自动转换不需要手动处理

super.single430 发表于 2019-10-12 18:04

requests.post(url, data=json.dumps(data)) ?

删掉丶关于n1 发表于 2019-10-12 18:08

super.single430 发表于 2019-10-12 18:04
requests.post(url, data=json.dumps(data)) ?

准备提交的内容是通过上条数据返回的内容进行组装提交比如data是上一条的数据获取的

SGC沉默 发表于 2019-10-12 19:37

序列化json就行

1170 发表于 2019-10-12 19:38

先把上条数据返回下次需要提交"result":后面的内容用json.loads提取出来,然后通过replace将"替换成\"然后在拼接提交

Light紫星 发表于 2019-10-12 20:36

data.replace('"','\\"')
页: [1]
查看完整版本: 如何利用python的优势巧妙的组装POST-data提交的数据?