FixXxeR FixXxeR - 1 year ago 156
HTTP Question

How to include body to a POST request in Python with requests

I need to extract all cruises from a page. I found that i need to make 2 requests. One for getting total results + reference to the individual cruise and another for the cruise itself.
So far i i successfully made a request and got a

. Problem is that The request returns only references contained in the first page.
Initially i used this:

as a
request URL. I am some headers in the request:

User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0
Accept: application/json, text/plain, */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Content-Type: application/json;charset=utf-8
Content-Length: 613
Cookie: ASP.NET_SessionId=zm50ahxa4uoiwcuowgkeizcn; SC_ANALYTICS_GLOBAL_COOKIE=f459da6904ee4cea8a809455f37b09c5|False; optimizelyEndUserId=oeu1480435687102r0.9194470051495017; optimizelySegments=%7B%223906442756%22%3A%22none%22%2C%223911484226%22%3A%22ff%22%2C%223917223299%22%3A%22direct%22%2C%223920524266%22%3A%22false%22%2C%224924982297%22%3A%22true%22%7D; optimizelyBuckets=%7B%227883701652%22%3A%227867701359%22%7D; _ga=GA1.3.1128287966.1480435688; AdBlockDetected=false; _msuuid_29439mm27589=D0670AB5-C96A-4706-A563-9F29FCA3D9D2; gwcc=%7B%22fallback%22%3A%221300159454%22%2C%22clabel%22%3A%22ykseCIGywFkQhoDuxQM%22%2C%22backoff%22%3A86400%2C%22backoff_expires%22%3A1480522087%7D; gaFindACruise=; _gat=1; optimizelyPendingLogEvents=%5B%22n%3Dengagement%26u%3Doeu1480435687102r0.9194470051495017%26wxhr%3Dtrue%26time%3D1480437791.708%26f%3D7274530066%2C7883701652%26g%3D3909534788%22%5D
Connection: keep-alive

Until now it worked if not counting the only 6 results i got back. They must be 106 at time of this post. The response contains metadata with total count(correct) and page number, total pages and more.
Then i saw this:
Firefox Screenshot
Request body
contains all i need for navigating in all the pages.
This is my entire code so far:

import requests

url = ""
session = requests.session()
data = {"searchParameters": {"p": [], "c": [], "d": [], "s": [], "ms": [], "adv": [], "sort": "dpa", "page": 5},
"renderingParameters": {"DefaultSortOption": "dpa", "LargeScreenFlag": "true", "NewModelIsLoading": "false",
"PagingAnchor": "", "ViewStyleSelectorVisible": "true", "FilterBarVisible": "true",
"NumberOfResultsAndSortByPanelVisible": "true", "DefaultResultsView": "Grid",
"MaxNumberOfResults": 0, "PaginationEnabled": "true", "KeepPageState": "true",
"PageSize": 9, "DefaultResultsGrouping": "Itinerary", "Duration": [],
"CruiseItinerary": [], "Voyage": [], "ExcludeVoyage": [], "PromoCode": [],
"AdditionalPromoCodes": []}}
headers = {"Host": "",
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0",
"Accept": "application/json, text/plain, */*", "Accept-Language": "en-US,en;q=0.5",
"Content-Type": "application/json;charset=utf-8",
"Cookie": "ASP.NET_SessionId=zm50ahxa4uoiwcuowgkeizcn; "
"SC_ANALYTICS_GLOBAL_COOKIE=f459da6904ee4cea8a809455f37b09c5|False; "
"optimizelyEndUserId=oeu1480435687102r0.9194470051495017; "
"%22%7D; optimizelyBuckets=%7B%227883701652%22%3A%227867701359%22%7D; "
"_ga=GA1.3.1128287966.1480435688; AdBlockDetected=false; "
"_msuuid_29439mm27589=D0670AB5-C96A-4706-A563-9F29FCA3D9D2; "
"%22backoff%22%3A86400%2C%22backoff_expires%22%3A1480522087%7D; gaFindACruise=; _gat=1; "
"Connection": "keep-alive"}
page =, headers=headers, data=data)
cruise_data = page.json()

page =, headers=headers, data=data)
cruise_data = page.json()

But now i am getting this error:

Traceback (most recent call last):
File "/home/fixxxer/PycharmProjects/POCruses/", line 31, in <module>
cruise_data = page.json()
File "/usr/lib/python3.5/site-packages/requests/", line 841, in json
return complexjson.loads(self.text, **kwargs)
File "/usr/lib/python3.5/json/", line 319, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.5/json/", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.5/json/", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I suppose i am doing the
part wrong. How to add this body in the request?
EDIT: If i remove the
parameter, request works fine but information is not what i need.IF i open the page with my browser and check the response sent to browser i get this:
Even this is different of what i get without the
. This is how i know that this
is crucial info.

Answer Source

It seams this page expect data as JSON so you need

page =, json=data)


Server always assigns new cookies to new client - especially cookie line ASP.NET_SessionId - so better GET main page to get new cookies before you do other requests.

After session.headers.update(headers) you don't have to use headers=headers in get()/post(). You may have to only change some headres if some request need some extra header.

If you use json= then it automatically add header "Content-Type": "application/json;charset=utf-8"

Full code could look like this:

import requests

# -- create session ---

session = requests.session()

headers = {
    "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0",
    "Accept": "application/json, text/plain, */*",
    "Accept-Language": "en-US,en;q=0.5",
    "Connection": "keep-alive"

# set headers for all requests

# --- get cookies ---

url = ""
page = session.get(url)

# --- search ---

data = {
    "searchParameters": {
        "p": [],
        "c": [],
        "d": [],
        "s": [],
        "ms": [],
        "adv": [],
        "sort": "dpa",
        "page": 5
    "renderingParameters": {
        "DefaultSortOption": "dpa",
        "LargeScreenFlag": "true",
        "NewModelIsLoading": "false",
        "PagingAnchor": "",
        "ViewStyleSelectorVisible": "true",
        "FilterBarVisible": "true",
        "NumberOfResultsAndSortByPanelVisible": "true",
        "DefaultResultsView": "Grid",
        "MaxNumberOfResults": 0,
        "PaginationEnabled": "true",
        "KeepPageState": "true",
        "PageSize": 9,
        "DefaultResultsGrouping": "Itinerary",
        "Duration": [],
        "CruiseItinerary": [],
        "Voyage": [],
        "ExcludeVoyage": [],
        "PromoCode": [],
        "AdditionalPromoCodes": []

url = ""

# `json=` add `"Content-Type": "application/json;charset=utf-8"`

page =, json=data)

cruise_data = page.json()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download