{"id":215990,"date":"2020-11-18T05:40:40","date_gmt":"2020-11-17T21:40:40","guid":{"rendered":"http:\/\/4563.org\/?p=215990"},"modified":"2020-11-18T05:40:40","modified_gmt":"2020-11-17T21:40:40","slug":"python-requests-%e5%ba%93%e5%8f%91%e9%80%81-body-%e5%8f%aa%e6%9c%89%e4%b8%80%e4%b8%aa-list-%e7%9a%84%e8%af%b7%e6%b1%82%e7%9a%84%e5%86%99%e6%b3%95","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=215990","title":{"rendered":"Python requests \u5e93\u53d1\u9001 body \u53ea\u6709\u4e00\u4e2a list \u7684\u8bf7\u6c42\u7684\u5199\u6cd5"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  Python requests \u5e93\u53d1\u9001 body \u53ea\u6709\u4e00\u4e2a list \u7684\u8bf7\u6c42\u7684\u5199\u6cd5               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : kickcandy <\/span>  <span><i><\/i> 3<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>[300004167]\uff0c\u53d1\u9001\u7684 body \u662f\u8fd9\u6837\u7684\u683c\u5f0f\uff0c\u5bf9\uff0c\u6ca1\u6709 key\uff0c\u53ea\u6709 value\uff0c\u67e5\u8fc7\u786e\u5b9e\u4e5f\u662f\u6b63\u786e\u7684 json \u683c\u5f0f\uff0c\u7528 postman \u53d1\u9001\u53ef\u4ee5\u6b63\u5e38\u8fd4\u56de\uff0c\u4f46\u662f\u7528 python \u7684 requests \u5e93\uff0c\u8bd5\u4e86\u597d\u51e0\u79cd\u5199\u6cd5\u90fd\u662f\u62a5\u9519\uff1a<\/p>\n<p>\u5404\u4f4d\u670b\u53cb\u6709\u6ca1\u6709\u597d\u7684\u529e\u6cd5\u89e3\u51b3\uff0c\u8c22\u8c22\uff01<\/p>\n<p>&#8212;-self.DeviceManage.send_post(url, json.dumps([300004167]), header)<\/p>\n<p>\u62a5\u9519\uff1ajson.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)<\/p>\n<p>&#8212;-self.DeviceManage.send_post(url, [str(300004167)], header)<\/p>\n<p>\u62a5\u9519\uff1aValueError: too many values to unpack (expected 2)<\/p>\n<p>&#8212;-self.DeviceManage.send_post(url, numpy.array([300004167]), header) #\u8bd5\u56fe\u8f6c\u6570\u7ec4<\/p>\n<p>\u62a5\u9519\uff1ajson.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>13<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4389502\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Pagliacii <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7528 `json.dumps` \u4e00\u4e0b\uff0c\u5982\u8fd9\u6837\uff1a![image.png]( https:\/\/i.loli.net\/2020\/12\/01\/WPFOI7G9UV3x1Ku.png)                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4389503\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Pagliacii <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Pagliacii #1 \u81f3\u4e8e\u4f60\u7528\u4e86 `json.dumps` \u62a5\u9519\uff0c\u5c31\u8981\u770b\u770b `send_post` \u662f\u600e\u4e48\u5199\u7684\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4389504\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : leavic <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             wireshark \u6293\u5305\u770b\u770b\u53d1\u51fa\u53bb\u7684\u6709\u4ec0\u4e48\u533a\u522b\u4e0d\u5c31\u77e5\u9053\u4e86\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4389505\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : kickcandy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Pagliacii <br \/>send_post \u65b9\u6cd5\u5c31\u662f\u8c03\u4e86 requests.post \u65b9\u6cd5\uff0c\u505a\u4e86\u4e00\u4e9b\u5904\u7406\u5c01\u88c5\uff1a<\/p>\n<p> def send_post(self, url, data, header):<br \/> result = requests.post(url=url, data=data, headers=header)<br \/> # res = result1.json()<br \/> result_str = json.dumps(result.json(), ensure_ascii=False, sort_keys=True, indent=2) # \u7ed3\u679c\u8f6c\u5b57\u7b26\u4e32<br \/> result_dict = json.loads(result_str) # \u7ed3\u679c\u8f6c\u5b57\u5178<br \/> return result, result_dict                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4389506\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zhijiansha <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u628a data \u6539\u6210 json=[123]\u8bd5\u8bd5\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4389507\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : binux <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7b2c\u4e00\u4e2a\u62a5\u9519\u662f result_dict = json.loads(result_str) \u548c\u4f60 post \u65e0\u5173                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4389508\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Pagliacii <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @kickcandy #4 \u90a3\u4f60\u5c31\u52a0\u4e0a\u4e00\u4e9b\u65e5\u5fd7\u6216\u8005 `print` \u8bed\u53e5\uff0c\u786e\u5b9a\u4e00\u4e0b\u5177\u4f53\u662f\u54ea\u91cc\u51fa\u9519\u4e86\u3002\u6bd4\u5982\uff1a<\/p>\n<p>&#8220;`<br \/>result = requests.post(url=url, data=data, headers=header)<br \/>print(f&#8221;{result.content}&#8221;)<br \/>result_str = json.dumps(result.json(), ensure_ascii=False, sort_keys=True, indent=2)<br \/>print(f&#8221;{result_str=}&#8221;)<br \/>result_dict = json.loads(result_str)<br \/>print(f&#8221;{result_dict=}&#8221;)<br \/>&#8220;`                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4389509\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : cryingsky <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8bf7\u6c42\u8fd4\u56de\u7ed3\u679c\u89e3\u6790\u6709\u95ee\u9898<br \/>\u8fd4\u56de\u6570\u636e\u5e94\u8be5\u4e0d\u662f json                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4389510\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : iyaozhen <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8fd9\u5c31\u662f send_post \u778e\u5c01\u88c5<\/p>\n<p>\u4e0d\u6539 send_post \u6e90\u7801\u7684\u8bdd\uff0cself.DeviceManage.send_post(url, json.dumps([300004167]), header)<br \/>header \u91cc\u9762\u52a0\u4e0a content type json                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4389511\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : kickcandy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @iyaozhen \u662f\uff0c\u53d1\u73b0\u4e86\u662f\u5c01\u88c5\u7684\u95ee\u9898\uff0c\u8fd4\u56de\u7684\u4e0d\u662f json \u683c\u5f0f\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4389512\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : kickcandy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @cryingsky \u8fd4\u56de\u7ed3\u679c\u4e0d\u662f json\uff0c\u610f\u5473\u7740\u8fd9\u4e2a\u63a5\u53e3\u4e0d\u89c4\u8303\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4389513\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : cryingsky <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @kickcandy #11 \u90a3\u5c31\u770b\u4f60\u63a5\u53e3\u89c4\u89c4\u8303\u600e\u4e48\u5b9a\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4389514\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : bnm965321 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5355 list \u7684 JSON \u6709\u5b89\u5168\u95ee\u9898\uff0c\u4e4b\u524d\u5728 tornado \u7684\u6587\u6863\u770b\u5230\u8fc7                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Python requests \u5e93&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/215990"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=215990"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/215990\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=215990"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=215990"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=215990"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}