将提琴手帖子转换为python request.post

时间:2018-11-16 18:25:37

标签: python request

我正在使用fiddler3捕获浏览器期间的数据流,并且我想使用python请求模数发表文章。

我在Fiddler中可以找到的帖子信息是:

URL:
https://shippingmanager.bpost.be/ShmFrontEnd/internal/110492/orders
Request Method: POST
Request Header:
Host: shippingmanager.bpost.be
Connection: keep-alive
Content-Length: 586
Origin: https://shippingmanager.bpost.be
language: en
User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36
Content-Type: application/json;charset=UTF-8
Accept: application/json, text/plain, */*
token: c087c667-7580-43b9-8bce-0c25f8377b8a
accountId: 110492
Referer: https://shippingmanager.bpost.be/ShmFrontEnd/start
Accept-Encoding: gzip, deflate, br
Accept-Language: en,en-US;q=0.9,zh-CN;q=0.8,zh;q=0.7,it;q=0.6,it-IT;q=0.5,it-CH;q=0.4
Cookie: JSESSIONID=3098DB975635FC531C284F1F5E1122D8.shm-v001330-frontend-pr-node4; _acl=YWRtaW46bm8=; _ga=GA1.3.809755681.1542326259; _gid=GA1.3.1523310150.1542326259

Request body:
{"reference":"1542362877BW24387","costCenter":"","totalPriceInEuroCent":"11000","weight":"2000","deliveryMethodId":7,"orderLines":[],"customer":{"firstName":"xhibg","lastName":"cdegg","company":"1988","street":"9 oxford street","streetNumber":"rm 3","box":"02138","postalCode":"02138","city":"cambridge","country":"US","language":"en","email":"jbgrveneio@foxairmail.com","phoneNumber":"8088254427","privateAddress":"true"},"selectedServices":[],"priceOverrides":[{"priceZone":"Z3","price":3400}],"extra":"","extraSecure":"","shopHandlingInstruction":"","additionalCustomerReference":""}

如果我尝试使用提琴手撰写POST,则响应看起来不错。但是,使用来自服务器的Python请求以及400状态代码时出现错误。这是我的python代码:

import requests
import bs4

post_url = ' https://shippingmanager.bpost.be/ShmFrontEnd/internal/110492/orders'  

headers ={"Host": "shippingmanager.bpost.be",
"Connection": "keep-alive",
"Content-Length": "586",
"Origin": "https://shippingmanager.bpost.be",
"language": "en",
"User-Agent": "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/66.0.3359.181 Safari/537.36",
"Content-Type": "application/json;charset=UTF-8",
"Accept": "application/json, text/plain, */*",
"token": "c087c667-7580-43b9-8bce-0c25f8377b8a",
"accountId": "110492",
"Referer": "https://shippingmanager.bpost.be/ShmFrontEnd/start",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en,en-US;q=0.9,zh-CN;q=0.8,zh;q=0.7,it;q=0.6,it-IT;q=0.5,it-CH;q=0.4",
}
cookies = {"JSESSIONID":"JSESSIONID=3098DB975635FC531C284F1F5E1122D8.shm-v001330-frontend-pr-node4",
          "_acl":"YWRtaW46bm8=",
          "_ga":"GA1.3.809755681.1542326259",
         "_gid":"A1.3.1523310150.1542326259"
          }

data = {"reference":"1542362877BW24387",
    "costCenter":"",
    "totalPriceInEuroCent":"11000",
    "weight":"2000",
    "deliveryMethodId":7,
    "orderLines":[],
    "customer"{"firstName":"xhibg","lastName":"cdegg","company":"1988","street":"9 oxford street","streetNumber":"rm 3","box":"02138","postalCode":"02138","city":"cambridge","country":"US","language":"en","email":"jbgrveneio@foxairmail.com","phoneNumber":"8088254427","privateAddress":"true"},
    "selectedServices":[],
    "priceOverrides":[{"priceZone":"Z3","price":3400}],
    "extra":"",
    "extraSecure":"",
    "shopHandlingInstruction":"",
    "additionalCustomerReference":""
    }

r = requests.post(post_url,headers=headers,data = data,cookies=cookies)
print (r.status_code)
soup = bs4.BeautifulSoup(r.text,'html.parser')
print (soup)

我想知道,可能我没有很好地格式化请求正文,但是我不知道如何解决。

1 个答案:

答案 0 :(得分:0)

您的网址中有多余的空格,可能就是这样 我写了一个很酷的提琴手扩展程序,用于将事务转换为python请求

https://github.com/yaronav/fiddlerRequestify