简体   繁体   中英

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:

在此处输入图像描述

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.

  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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM