Wednesday, 28 July 2021

POST two form inputs with same name but different type

I have a webpage that submits two inputs in a form. My browser's POST can be seen below.
The first input is the path to the current file uploaded to the site (type="hidden"); the second input is the new file to be uploaded (type="file").

-----------------------------334620328614915644833547167693
Content-Disposition: form-data; name="image1"

data/file.jpg
-----------------------------334620328614915644833547167693
Content-Disposition: form-data; name="image1"; filename=""
Content-Type: application/octet-stream

These inputs have the same name but are typed differently, which causes problems with my understanding of the requests library. As far as I know, to submit data, you POST a dictionary with the name of the input and its new value.

data = {}

form = get_all_forms(url)[1] #function returns list of forms gained using requests.get() - we select the correct form
formDetails = get_form_details(form) #uses BeautifulSoup to find all input values
for inputTag in formDetails["inputs"]:
  if inputTag["type"] == "file":
    pass #doesn't send anything
  else:
    data[inputTag["name"]] = inputTag["value"] #sets to current value

res = requests.post(url, data=data)

The above code will POST a dictionary with 'image1':'data/file.jpg'. Unfortunately, the page now thinks that there is a new file being uploaded called data/file.jpg, and when it can't find that file, it deletes the current image.

How do I POST separate values for type="file" and type="hidden"?



from POST two form inputs with same name but different type

No comments:

Post a Comment