I want to create a post request like the following picture in python that return data as I received in a browser :
And cookie is as follow: For this, I have written the following code:
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)
But I don't receive the right information from the server.
CodePudding user response:
you need to post an
application/json
request so use thejson
parameter forrequests.post()
the api you're communicating with seems to require some sort of authentication, try to transplant the session cookie with the
cookies
parameterdata = {...} cookies = {"_session": "1ac[..]"} response = requests.post(url, json=data, cookies=cookies)
CodePudding user response:
This will give you what you want:
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'])
Note: The session cookie used in this answer may expire. So, although it works now, it may not always work