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

2
  • The other fields are blank strings, not the string "None". The field names are otherwise correct. Commented Oct 21, 2015 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. Commented Oct 21, 2015 at 12:52

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, this worked i.e using "" and also changing the parameter from 'payload' to 'data'. Thank you Martijn.

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.