Kiro
Kiro

Reputation: 13

Post request with arrays using python requests

How can i create a post request like this with python requests?

$url = 'https://joinposter.com/api/incomingOrders.createIncomingOrder'
 . '?token=687409:4164553abf6a031302898da7800b59fb';

$incoming_order = [
    'spot_id'   => 1,
    'phone'     => '+380680000000',
    'products'  => [
        [
            'product_id' => 169,
            'count'      => 1
        ],
    ],
];

$data = sendRequest($url, 'post', $incoming_order);

I've tried to do it like this:

payload = {'token': 687409:4164553abf6a031302898da7800b59fb,
                          'spot_id': 1, 'phone': '+380680000000', 'products': {'product_id': 169, 'count': 1}}

r = requests.post('https://joinposter.com/api/incomingOrders.createIncomingOrder', params=payload)

But it didn't work. Parameter 'products' is not created right. This is how the created URL looks like: https://joinposter.com/api/incomingOrders.createIncomingOrder?token=704698%3A8544082b36a413a51b5c8c3ce0e2b162&spot_id=1&phone=%2B380680000000&products=product_id&products=count

So how can i create a post request with arrays of arrays in it?

Upvotes: 1

Views: 248

Answers (1)

Harshana
Harshana

Reputation: 5496

Try seperating your token and incoming_order into two attributes:

import requests

url = "https://joinposter.com/api/incomingOrders.createIncomingOrder"
params = {'token': "xxxxxxxxxxxxxxxxxxxxx"}

incoming_order = {'spot_id': 1, 'phone': '+380680000000', 'products': {'product_id': 169, 'count': 1}}

r = requests.post(url=url, params=params, json=incoming_order)

print(r.status_code)

Replace the token with a valid token.

Upvotes: 1

Related Questions