Julio Galan
Julio Galan

Reputation: 134

Need Help Sending Data As 'POST' Request

I have a script which finds a specific link with a product page on a website and I want to be able to check out. Everything works until I have my POST request with the data (itemId, quantity, sku).

I'm not entirely sure what I'm doing wrong here so I was hoping someone could help.

Code:

def checkout_item(item_url):
    website = requests.get(item_url)
    document = Soup(website.content, "html.parser")

    print('Now in product page')

    item_id = document.find('body').get('id').replace('item-', '')
    item_sku = ''

    product_variants_string = document.find('div', class_='product-variants').get('data-variants')
    product_variants_json = json.loads(product_variants_string)

    for json_element in product_variants_json:
        attributes_json = json_element['attributes']
        if attributes_json['Size'] is not f'{size_to_buy}':
            continue
        else:
            item_sku = json_element['sku']

    data = {
        'additionalFields': 'null',
        'itemId': f'{item_id}',
        'quantity': '1',
        'sku': f'{item_sku}'
    }

    print(data)

    checkout_website = requests.post(target_site + '/checkout', data=data)
    checkout_document = Soup(checkout_website.content, "html.parser")

    if checkout_document is None:
        print('checkout_document is null')
    else:
        print(checkout_document)


Item I'm using for a test checkout: https://www.icantdecideyet.com/join/preorderbemyenemy-flag-printed-hoodie

Output: http://prntscr.com/ozuto0

Upvotes: 3

Views: 45

Answers (1)

Calculus
Calculus

Reputation: 781

instead of

checkout_website = requests.post(target_site + '/checkout', data=data)

try

checkout_website = requests.post(target_site + '/checkout', data=json.dumps(data))
print(str(checkout_website.status_code))

Upvotes: 1

Related Questions