如何使用负载请求在 python 中创建 post 请求?
How to create a post request in python with payload request?
我想创建一个 post 请求,如下图 python 中的 return 我在浏览器中收到的数据:
cookie如下:
为此,我编写了以下代码:
import requests
url = "https://flight-api-v1.utravs.com/Flight/statistic/FlightPriceStatistics"
data = {
"minimumPriceStatisticRequest": {
"$id": 1,
"availabilityRequest": {
"$id": 2,
"segments": {
"$id": 3,
"$values": [
{
"$id": 4,
"destination": "KIH-Kish-all",
"origin": "THR-Tehran-all",
"departureDateTime": "2021-12-02T00:00:00.000Z",
"uniqueIndex": 0
}
]
},
"passengers": {
"$id": 5,
"$values": [
{
"$id": 6,
"type": 1,
"quantity": 1,
"optionalServices": {
"$id": 7,
"$values": []
}
}
]
},
"travelDetails": {
"$id": 8,
"cabinType": 1,
"airTripType": 1,
"stopQuantityType": 3,
"pricingSourceType": 3
},
"availabilityType": 0
},
"minRange": 10,
"maxRange": 10
}
}
x = requests.post(url, data=data)
print(x.text)
但是我没有从服务器收到正确的信息。
您需要 post 一个 application/json
请求,因此请为 requests.post()
使用 json
参数
您正在与之通信的 api 似乎需要某种身份验证,请尝试使用 cookies
参数
移植会话 cookie
data = {...}
cookies = {"_session": "1ac[..]"}
response = requests.post(url, json=data, cookies=cookies)
这会给你想要的:
import requests
url = "https://flight-api-v1.utravs.com/Flight/statistic/FlightPriceStatistics"
data = {
"minimumPriceStatisticRequest": {
"$id": 1,
"availabilityRequest": {
"$id": 2,
"segments": {
"$id": 3,
"$values": [
{
"$id": 4,
"destination": "KIH-Kish-all",
"origin": "THR-Tehran-all",
"departureDateTime": "2021-12-02T00:00:00.000Z",
"uniqueIndex": 0
}
]
},
"passengers": {
"$id": 5,
"$values": [
{
"$id": 6,
"type": 1,
"quantity": 1,
"optionalServices": {
"$id": 7,
"$values": []
}
}
]
},
"travelDetails": {
"$id": 8,
"cabinType": 1,
"airTripType": 1,
"stopQuantityType": 3,
"pricingSourceType": 3
},
"availabilityType": 0
},
"minRange": 10,
"maxRange": 10
}
}
with requests.Session() as session:
cookies = {"_session": "1acda9e8-3051-47bb-bddf-9d68553ebbee"}
headers = {"Accept": "application/json"}
(x := session.post(url, json=data, cookies=cookies, headers=headers)).raise_for_status()
print(x.json()['Result'])
注意:此回答中使用的会话 cookie 可能会过期。所以,虽然它现在有效,但可能并不总是有效
我想创建一个 post 请求,如下图 python 中的 return 我在浏览器中收到的数据:
cookie如下:
import requests
url = "https://flight-api-v1.utravs.com/Flight/statistic/FlightPriceStatistics"
data = {
"minimumPriceStatisticRequest": {
"$id": 1,
"availabilityRequest": {
"$id": 2,
"segments": {
"$id": 3,
"$values": [
{
"$id": 4,
"destination": "KIH-Kish-all",
"origin": "THR-Tehran-all",
"departureDateTime": "2021-12-02T00:00:00.000Z",
"uniqueIndex": 0
}
]
},
"passengers": {
"$id": 5,
"$values": [
{
"$id": 6,
"type": 1,
"quantity": 1,
"optionalServices": {
"$id": 7,
"$values": []
}
}
]
},
"travelDetails": {
"$id": 8,
"cabinType": 1,
"airTripType": 1,
"stopQuantityType": 3,
"pricingSourceType": 3
},
"availabilityType": 0
},
"minRange": 10,
"maxRange": 10
}
}
x = requests.post(url, data=data)
print(x.text)
但是我没有从服务器收到正确的信息。
您需要 post 一个
使用application/json
请求,因此请为requests.post()
json
参数您正在与之通信的 api 似乎需要某种身份验证,请尝试使用
移植会话 cookiecookies
参数data = {...} cookies = {"_session": "1ac[..]"} response = requests.post(url, json=data, cookies=cookies)
这会给你想要的:
import requests
url = "https://flight-api-v1.utravs.com/Flight/statistic/FlightPriceStatistics"
data = {
"minimumPriceStatisticRequest": {
"$id": 1,
"availabilityRequest": {
"$id": 2,
"segments": {
"$id": 3,
"$values": [
{
"$id": 4,
"destination": "KIH-Kish-all",
"origin": "THR-Tehran-all",
"departureDateTime": "2021-12-02T00:00:00.000Z",
"uniqueIndex": 0
}
]
},
"passengers": {
"$id": 5,
"$values": [
{
"$id": 6,
"type": 1,
"quantity": 1,
"optionalServices": {
"$id": 7,
"$values": []
}
}
]
},
"travelDetails": {
"$id": 8,
"cabinType": 1,
"airTripType": 1,
"stopQuantityType": 3,
"pricingSourceType": 3
},
"availabilityType": 0
},
"minRange": 10,
"maxRange": 10
}
}
with requests.Session() as session:
cookies = {"_session": "1acda9e8-3051-47bb-bddf-9d68553ebbee"}
headers = {"Accept": "application/json"}
(x := session.post(url, json=data, cookies=cookies, headers=headers)).raise_for_status()
print(x.json()['Result'])
注意:此回答中使用的会话 cookie 可能会过期。所以,虽然它现在有效,但可能并不总是有效