如何使用表单数据在python中发送post请求?

时间:2017-09-18 11:08:02

标签: python python-requests

我想使用rest api并发送格式为:

data[Passenger][0][passportnumber]:
data[Passenger][0][fname]:
data[Reservation][id]:6113081
data[Passenger][0][latinfname]:faezeh
data[Passenger][0][age]:adl

当我用邮递员发送它时,我得到了我想要的答案,但是当我用pyhton发送此请求时,我无法得到正确的答案。我的python请求:

 data = {
  'data[Reservation][id]':6112211,
  'data[Passenger][0][latinfname]':'faeze',
  'data[Passenger][0][age]':'adl'}
 response = requests.post("https://travel.ir/api" , data = data, auth=('user', 'password'))

ps:当邮递员将我的请求转换为python代码时,它会返回如下内容:

    import requests

url = "https://travel.ir/api"

querystring = {"token":"token","member_id":"member_id"}

payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"data[Passenger][0][passportnumber]\"\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"data[Passenger][0][fname]\"\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"data[Reservation][id]\"\r\n\r\n6113081\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"data[Passenger][0][latinfname]\"\r\n\r\nfaezeh\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"data[Passenger][0][age]\"\r\n\r\nadl\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"data[Passenger][0][birthday]\"\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"data[Passenger][0][passportplaceissue]\"\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"data[Passenger[0][lname]\"\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"data[Passenger][0][latinlname]\"\r\n\r\nfaeze\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"data[Passenger][0][gender]\"\r\n\r\n1\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"data[Reservetion][email]\"\r\n\r\mail@gmail.com\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"data[Passenger][0][nationalcode]\"\r\n\r\n1111111111\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"data[Passenger][0][nationality]\"\r\n\r\nIRN\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"data[Passenger][0][passportexpdate]\"\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"
headers = {
    'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
    'authorization': "Basic wwwwwww",
    'cache-control': "no-cache",
    'postman-token': "wwwwwww"
    }

response = requests.request("POST", url, data=payload, headers=headers, params=querystring)

print(response.text)

2 个答案:

答案 0 :(得分:0)

我最初省略了空白值,我尝试发送空字符串而不是没有值,它运行正常。

答案 1 :(得分:-1)

使用json.dumps()

在发送之前转换数据
response = requests.post("https://travel.ir/api", data=json.dumps(data), auth=('user', 'password'))