Monday 9 September 2019

Trouble getting desired response issuing a post requests

I've created a script in python to get a 200 status code issuing a post http requests but when I run my script I get 403 instead. It seems that I followed the way how the requests is being sent in chrome dev tools.

To do it manually - go to that page, select 6 as size and then hit the add to cart button.

How can I do the same using the script below?

Webpage address

I've tried with:

import requests
from bs4 import BeautifulSoup

main_url = 'https://www.footlocker.co.uk/en/homepage'
post_url = 'https://www.footlocker.co.uk/en/addtocart?'

params = {
    'SynchronizerToken': '',
    'Ajax': True,
    'Relay42_Category': 'Product Pages',
    'acctab-tabgroup-314207586604090': None,
    'Quantity_314207586604070': '1',
    'SKU': '314207586604070'
}

with requests.Session() as s:
    r = s.get(main_url)
    soup = BeautifulSoup(r.text,"lxml")

    #parsing token to reuse within data
    token = soup.select_one("[name='SynchronizerToken']")['value']

    params['SynchronizerToken'] = token

    res = s.post(post_url,params=params,data=params,headers={
        'user-agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36',
        'x-requested-with': 'XMLHttpRequest',
        'referer': 'https://www.footlocker.co.uk/en/p/nike-signal-dmsx-men-shoes-73190?v=314207586604',
        'accept': 'application/json, text/javascript, */*; q=0.01'
        })
    print(res.status_code)

Current status:

403

Expected status:

200



from Trouble getting desired response issuing a post requests

1 comment:

  1. Nice article. It's very helpful to me. Thank you for share with us. If you want to remove line breaks from paragraph then check Remove Line Breaks Online Tool.

    ReplyDelete