0

Trying to use the Python Requests module to post to a form:

using Chrome/inspect/network tab after I search for something is shows:

Request URL:https://www.fbo.gov/index?s=opportunity&mode=list&tab=search Status Code:302 Found

The Request Payload is:

... begin snippet ....

procurement_notice ------WebKitFormBoundary9stOUHDiQLr2yrEB Content-Disposition: form-data; name="dnf_class_values[procurement_notice][notice_id]"

8045295a672345856701e8ff0ab87a4c ------WebKitFormBoundary9stOUHDiQLr2yrEB Content-Disposition: form-data; name="dnf_class_values[procurement_notice][_so_agent_save_agent]"

------WebKitFormBoundary9stOUHDiQLr2yrEB Content-Disposition: form-data; name="dnf_class_values[procurement_notice][custom_response_date]"

------WebKitFormBoundary9stOUHDiQLr2yrEB Content-Disposition: form-data; name="dnf_class_values[procurement_notice][custom_posted_date]"

30 ------WebKitFormBoundary9stOUHDiQLr2yrEB Content-Disposition: form-data; name="dnf_class_values[procurement_notice][keywords]"

Colorado

... end snippet ....

The code, I am trying is:

 import requests


 headers ={'X-Requested-With': 'XMLHttpRequest',
                 'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8 GTB7.1 (.NET CLR 3.5.30729)'}
 url = 'https://www.fbo.gov/index?s=opportunity&mode=list&tab=search'
 search_data={"dnf_class_values[procurement_notice][keywords]":Colorado}
 r = requests.post(url, data=search_data, headers=headers)

it doesn't give me the r.content as if I passed in the search term 'Colorado'.

So my question is does the 'Code:302', which is a re-direct, mean they don't accept a POST request and also how do I deal with the 'WebKitFormBoundary' ...'Content-Disposition: form-data...' as it doesn't look like a normal dictionary element I can pass in.

Martin Gergov
  • 1,556
  • 4
  • 20
  • 29
R. Yora
  • 31
  • 9
  • When you send a Python dictionary via POST, the dict needs to be encoded. `urllib` by default will send `application/x-www-form-urlencoded` data. The content-type of the request, however, is probably `multipart/form-data` and the content also looks appropriate. To get `multipart/form-data` the [request must be done differently](http://stackoverflow.com/questions/12385179/how-to-send-a-multipart-form-data-with-requests-in-python). – dhke Oct 12 '15 at 10:26
  • Yes, the content-type shows " Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryRERVZlzvAeLMVXRm " – R. Yora Oct 12 '15 at 12:39
  • Possible duplicate of [python requests - POST Multipart/form-data without filename in HTTP request](http://stackoverflow.com/questions/23120974/python-requests-post-multipart-form-data-without-filename-in-http-request) – dhke Oct 12 '15 at 12:48

0 Answers0