Skip to content

Instantly share code, notes, and snippets.

@srafay
Created January 16, 2019 05:18
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save srafay/19e0a13fe7e402f0a79715b1ed3f6560 to your computer and use it in GitHub Desktop.
Save srafay/19e0a13fe7e402f0a79715b1ed3f6560 to your computer and use it in GitHub Desktop.
Python Request for form-data
# Generates request data payload to be sent as 'form-data'
REQUEST_FORM_DATA_BOUNDARY = "REQUEST_FORM_DATA_BOUNDARY"
FORM_DATA_STARTING_PAYLOAD = '--{0}\r\nContent-Disposition: form-data; name=\\"'.format(REQUEST_FORM_DATA_BOUNDARY)
FORM_DATA_MIDDLE_PAYLOAD = '\"\r\n\r\n'
FORM_DATA_ENDING_PAYLOAD = '--{0}--'.format(REQUEST_FORM_DATA_BOUNDARY)
REQUEST_CUSTOM_HEADER = {
'content-type': "multipart/form-data; boundary={}".format(REQUEST_FORM_DATA_BOUNDARY),
'Content-Type': "",
'cache-control': "no-cache"
}
def generate_form_data_payload(kwargs):
payload = ''
for key, value in kwargs.items():
payload += '{0}{1}{2}{3}\r\n'.format(FORM_DATA_STARTING_PAYLOAD, key, FORM_DATA_MIDDLE_PAYLOAD, value)
payload += FORM_DATA_ENDING_PAYLOAD
return payload
# Pass your input as kwargs to the function
# Example:
import requests
API_URL = "www.example.com/verifyuser
kwargs = {'ChannelId': channel_id,
'ReferenceNumber': reference_number,
'AccountNumber': account_numbers,
'Cnic': cnic,
'MobileNumber': mobile_number,
'Otp': otp}
# generate payload to be sent as form-data
request_data = generate_form_data_payload(kwargs)
response = requests.post(API_URL, headers=REQUEST_CUSTOM_HEADER, data=request_data)
@Camps94
Copy link

Camps94 commented Sep 16, 2020

Hey! Great script! Would you know how to send the datepicker in the form data to check Electricity Balance in this webpage: https://www.centrodeinformacao.ren.pt/PT/InformacaoExploracao/Paginas/EstatisticaDiaria.aspx

The value is sent in here but I dont know how to send it in a request.post:

Thank you very much

image

@srafay
Copy link
Author

srafay commented Apr 25, 2021

Hey! Great script! Would you know how to send the datepicker in the form data to check Electricity Balance in this webpage: https://www.centrodeinformacao.ren.pt/PT/InformacaoExploracao/Paginas/EstatisticaDiaria.aspx

The value is sent in here but I dont know how to send it in a request.post:

Thank you very much

image

Hey sorry I just saw this comment (GitHub doesn't notify you when someone comments on your gists). Were you able to solve your problem? If you did, let me know how so that others facing the same problem could solve it as well.
Thanks! @Camps94

@Camps94
Copy link

Camps94 commented Apr 29, 2021

Hello Syed,
Yes, I solved easily using Selenium. Specifically; from selenium.webdriver.common.keys import Keys.
I declare a variable type DATE. And then i do the following:
date = single_date.strftime("%d-%m-%Y") elem = driver.find_element_by_name(The whole datpicker until foo included) elem.clear() elem.send_keys(date) elem.send_keys(Keys.RETURN)

And that's it! if its not cleared let me know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment