0

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..

R. Yora
  • 31
  • 9
  • The other fields are *blank strings*, not the string `"None"`. The field names are otherwise correct. – Martijn Pieters Oct 21 '15 at 12:52
  • The brackets are a Ruby-on-Rails and PHP convention; there is no standard describing these, but square brackets are parsed by such servers to produce a nested array structure. – Martijn Pieters Oct 21 '15 at 12:52

1 Answers1

1

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)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Yes, this worked i.e using "" and also changing the parameter from 'payload' to 'data'. Thank you Martijn. – R. Yora Oct 29 '15 at 09:12