R. Yora
R. Yora

Reputation: 31

Python and Requests module, correct syntax for 'payload' in url-encoded POST method

I'm using the Requests module and Python 3. when I inspect in Chrome, If the parsed form data is listed as:

   list_class_values[notice][zipstate]:
   list_class_values[notice][type][]:
   list_class_values[notice][keywords]:colorado 

In the above case, I'm searching for 'colorado'. What is the proper syntax to list them in the 'payload' section, given the below code snippet? the content-type is "application/x-www-form-urlencoded".

payload = {"list_class_values[notice][zipstate]":"None", "list_class_values[notice][type][]":"None", "list_class_values[notice][keywords]":"colorado"}
r = requests.post(url='http://www.example.com', payload=payload, headers=headers)
print(r.content)

Do I need a tuple in there somewhere? e.g. "list_class_values(notice,keywords)":"colorado" ? as the data doesn't change when I change the keyword..

Upvotes: 0

Views: 1610

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121864

I think it's the other fields that are the issue here. Their values are empty strings, not the string "None":

payload = {
    "list_class_values[notice][zipstate]": "",
    "list_class_values[notice][type][]": "",
    "list_class_values[notice][keywords]": "colorado"
}

The form field names are otherwise correct; the syntax is a convention used by Ruby on Rails and PHP, but is otherwise not a standard. Servers that support the syntax parse the keys out into array maps (dictionaries in Python terms). See Form input field names containing square brackets like field[index]

Note that you need to pass this to the data argument for a POST body (there is no payload keyword argument, you should get an exception):

r = requests.post(url='http://www.example.com', data=payload, headers=headers)

Upvotes: 1

Related Questions