简体   繁体   English

如何使用有效负载请求在 python 中创建发布请求?

[英]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:我想在 python 中创建一个如下图所示的发布请求,该请求返回我在浏览器中收到的数据:

在此处输入图像描述

And cookie is as follow: cookie如下: 在此处输入图像描述 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()您需要发布application/json请求,因此请使用requests.post()json参数

  2. the api you're communicating with seems to require some sort of authentication, try to transplant the session cookie with the cookies parameter您正在与之通信的 api 似乎需要某种身份验证,尝试使用cookies参数移植 session cookie

     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.注意:此答案中使用的 session cookie 可能会过期。 So, although it works now, it may not always work所以,虽然它现在有效,但它可能并不总是有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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