Sending a POST request with short values ​​for the same key with the request library

How would you send a query with short values ​​with the same key?

r = requests.post('http://www.httpbin.org/post', data={1: [2, 3]}) print r.content 
  {
   ...
   "form": {
     "13"
   }, 
   ...
 }

Edit:

Hmm, very strange. I tried repeating these messages with a simple Flask application, and I get:

 [('1', u'2'), ('1', u'3')] 

Is this just a flaw in httpbin.org?

+4
source share
2 answers

It turns out that requests sent POST data without problems. This was a problem at http://httpbin.org that caused the form data to be smoothed, and several values ​​with the same key were ignored.

+4
source

Try Werkzeug MultiDict . This is the same structure used for this purpose in Flask applications.

 import requests from werkzeug.datastructures import MultiDict data = MultiDict([('1', '2'), ('1', '3')]) r = requests.post('http://www.httpbin.org/post', data=data) print(r.content) 

Result:

 ... "form": { "1": [ "2", "3" ] }, ... 
+3
source

Source: https://habr.com/ru/post/1384817/


All Articles