Search code examples
pythonjsonpostgetrequest

How to create a post request in python with payload request?


I want to create a post request like the following picture in python that return data as I received in a browser :

enter image description here

And cookie is as follow: enter image description here 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.


Solution

    1. you need to post an application/json request so use the json parameter for requests.post()

    2. the api you're communicating with seems to require some sort of authentication, try to transplant the session cookie with the cookies parameter

      data = {...}
      cookies = {"_session": "1ac[..]"}
      response = requests.post(url, json=data, cookies=cookies)