drakonli
drakonli

Reputation: 2414

How to send multidimensional POST in Python

I use requests.api lib to send post request. What I want is to send multidimensional POST data and I always come up short with this code:

import requests

url = 'http://someurl.com';

request_data = {}
request_data['someKey'] = 'someData'
request_data['someKeytwo'] = 'someData2'
request_data['requestData'] = {'someKey3': 'someData3'}

login = requests.post(url, data=login_data)

On the receiving end i get a POST with "requestData" => "someKey3" instead of "requestData" => ["someKey3" => 'someData3']

How do I send the correct POST?

Upvotes: 5

Views: 1360

Answers (2)

drakonli
drakonli

Reputation: 2414

The correct answer for my question is:

import requests

url = 'http://someurl.com';

request_data = {}
request_data['requestData[someKey3]'] = 'someData3'

login = requests.post(url, data=request_data)

Upvotes: 3

Netwave
Netwave

Reputation: 42678

Simply use:

import json
login = requests.post(rul, data=json.dumps(login_data))

This way you receive a json on the the receiving side.

Upvotes: 1

Related Questions