0

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.

Rahman
  • 410
  • 6
  • 26
  • 1
    Does this answer your question? [How to POST JSON data with Python Requests?](https://stackoverflow.com/questions/9733638/how-to-post-json-data-with-python-requests) – vinzenz Dec 02 '21 at 12:01
  • @vinzBad Unfortunately, No. u can test it in python. the site is online. – Rahman Dec 02 '21 at 12:07
  • please try requests.post(url, json=data) – vinzenz Dec 02 '21 at 12:08
  • I get a SessionAccessDenied Error when posting with json=data, probably your code is missing some authentication step. You can try transplanting the cookies from your browser request. – vinzenz Dec 02 '21 at 12:11
  • Looks like you need to provide some kind of credentials. I tried your code and get HTTP 200 but with this text "{"Success":false,"FailedReason":23,"Error":{"Code":23,"Message":"There is no error description available for SessionAccessDenied","HaveToBlockCredit":false},"Validations":[]}" –  Dec 02 '21 at 12:11
  • @DarkKnight So, what should I do? – Rahman Dec 02 '21 at 12:13
  • You'll need to review the documentation for that API –  Dec 02 '21 at 12:14
  • @DarkKnight But I can browse by a browser without any credentials! – Rahman Dec 02 '21 at 12:16
  • @DarkKnight I want to crawl this page. – Rahman Dec 02 '21 at 12:16
  • @vinzBad I added the cookie picture in the post. please tell me what should I do? – Rahman Dec 02 '21 at 12:19
  • If you try to use that URL in a browser you will get a different error. Try it in something like Postman and you'll see the error –  Dec 02 '21 at 12:21
  • @DarkKnight Yes. you are right. But when I paste the URL in browser it send a get request instead of a POST. – Rahman Dec 02 '21 at 12:24
  • please redact the cookie values as they contain potential sensitive info – vinzenz Dec 02 '21 at 12:28
  • Of course the browser issues a GET but you said "But I can browse by a browser..." which has nothing to do with issuing a POST. I was merely pointing out that you can **not** do this via a browser –  Dec 02 '21 at 12:33

2 Answers2

1
  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)
    
vinzenz
  • 669
  • 3
  • 14
1

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