0

I'm trying use python requests to submit the following form:

<form method="post" action="deliverData.php" enctype="multipart/form-data">
<p><input type="checkbox" name="Description" value="Yes"> Include Course Description&nbsp;&nbsp;&nbsp;
<p><input type="checkbox" name="Extended" value="Yes"> Include Additional Class Meetings&nbsp;&nbsp;
...
<input type="hidden" name="Group" value="Mathematics" />

When I inspect the post request payload in a browser, I get something like the following payload:

------WebKitFormBoundaryNfRaBsIDJ61noG2M
Content-Disposition: form-data; name="Description"

Yes
------WebKitFormBoundaryNfRaBsIDJ61noG2M
Content-Disposition: form-data; name="Extended"

Yes
------WebKitFormBoundaryNfRaBsIDJ61noG2M
...

I successfully receive a response from the server with the following code:

headers = {"Content-Type": "multipart/form-data; boundary=----WebKitFormBoundaryNfRaBsIDJ61noG2M"}
data = ''' <payload from above> '''
response = requests.post(base_url, headers=headers, data=data)

But when I try to use json like:

json1 = {
'Description': 'Yes',
'Extended': 'Yes',
'InstructionMode': 'Yes',
...}
response = requests.post(base_url, headers=headers, data=json1)

There is a server error (Error 500). I can use f'string' to vary the parameters in the payload, but this can't be the correct way to use post requests. What's the proper way to submit this form?

sogaj53
  • 1
  • 1
  • @RandomDavis Error 500 – sogaj53 Sep 29 '22 at 21:40
  • Post the full response from the server. Surely there's a response body, and not literally just error code 500 with nothing else. – Random Davis Sep 29 '22 at 21:41
  • @RandomDavis after hours of trying, it suddenly started behaving as expected after posting this question – sogaj53 Sep 29 '22 at 21:52
  • It was originally "500 Server Error: Internal Server Error for url" – sogaj53 Sep 29 '22 at 21:53
  • the server expects the data in a particular format, which you have defined in the header (you might want to check out [What is the boundary in multipart/form-data?](https://stackoverflow.com/questions/3508338/what-is-the-boundary-in-multipart-form-data)). As long as you're using that set of headers, you must format your payload like that. It's possible that the server isn't set up to accept data in any other form, so changing headers is not guaranteed to help either. – Driftr95 Sep 30 '22 at 16:00

0 Answers0