0

I am trying to learn how the headers are being constructed when a zipcode is entered by the user and a "POST" command is issued (by clicking on the "Shop Now" button) from the following website:

enter image description here

I believe the interesting part of this "POST" request is how the site is forming the following headers but I can't figure out how it is doing it (my suspicion is that there is some JavaScript/Angular code that is responsible):

x-ccwfdfx7-a
x-ccwfdfx7-b
x-ccwfdfx7-c
x-ccwfdfx7-d
x-ccwfdfx7-f
x-ccwfdfx7-z

So I have tried to use the requests module to login as guest to learn more about how this flow works:

  1. with requests.Session()
  2. with cloudscraper.create_scraper()

So far all my attempts have FAILED. Here is my code:

import requests  
from requests_toolbelt.utils import dump   #pip install requests_toolbelt
import cloudscraper   #pip install cloudscraper

#with requests.Session() as session:
with cloudscraper.create_scraper(
        browser={
            'custom': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36'
        }
    ) as session:

        CITY = XXXXX
        ZIPCODE = XXXXX

        #get cookies
        url = 'http://www.peapod.com'
        res1 = session.get(url)
        session.headers['Referer'] = 'https://www.peapod.com/'

        #get more cookies
        url = 'http://www.peapod.com/login'
        res2 = session.get(url)

        #get more cookies
        url = 'https://www.peapod.com/ppd/bundles/js/ppdBundle.js'
        res3 = session.get(url)

        #get all the service locations
        response = session.get('https://www.peapod.com/api/v4.0/serviceLocations',
            params={
                'customerType': 'C',
                'zip': ZIPCODE
            }
        )

        try:
            loc_id = list(
                filter(
                    lambda x: x.get('location', {}).get('city') == CITY, response.json()['response']['locations']
                )
            )[0]['location']['id']
        except IndexError:
            raise ValueError("Can't find City '{}' -> Zip {}".format(CITY, ZIPCODE))

        #login as guest
        response = session.post('https://www.peapod.com/api/v4.0/user/guest',
            json={
                'customerType': 'C',
                'cities': None,
                'email': None,
                'serviceLocationId': loc_id,
                'zip': ZIPCODE
            },
            params={
                'serviceLocationId': loc_id,
                'zip': ZIPCODE
            }
        )

This seems to produce some sort of an error message saying "I'm blocked" which I believe is due to the fact that I can't figure out how the browser constructs the ccwfdfx7headers in the "POST" request (my suspicion is that there is some JavaScript/Angular code that is responsible for constructing these headers but I can't find it and hoping someone could help...)

On the same computer, Chrome browser is able to login just fine

Denis
  • 11,796
  • 16
  • 88
  • 150

0 Answers0