1

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?

1
  • HTTP is a text only protocol. The most common way to convert non-strings into strings is by using json.dumps(...) which converts the dictionary into a text format. It is then up to the server to convert it back. Commented Nov 21, 2020 at 21:17

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I was sending data=incoming_order and thanks for keeping the solution neat and clean.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.