Great post, James! I needed to do a similar task with google forms, and this was very helpful. However, it didn't work right off the bat for me. After using requests.post(url, data=d), I got the status code 401 (which stands for an Unauthorized Error).
Side Note: no error is actually thrown. To view the status code, print the object or use r.status_code.
However, I was able to find a solution, and for the sake of the other readers I wanted to share it here. Here's what I did:
First, I filled out the form using chrome. After submitting the form, I used the developer tools in Chrome (on the landing page of the form) to look at the headers. That can be found on the Network tab after selecting the formResponse entry. For anyone following along, there should be a list of several header categories including General, Response Headers, Request Headers, and Form Data.
Another Side Note: all of the field names with the data you entered in the Google Form can be found under Form Data; I found that using this was a lot easier than hunting through html tag attributes.
Anyways, Request Headers is the one you want. Now, I'm not sure if you need every single field under the Request Headers, but I decided to use all of them. To do this, I just copied the plain text under Request Headers and assigned it to a variable like this:
header_text='''
field_one: value
field_two: value
etc.
'''
Then, I parsed the string using this function:
defparse_headers(text):# Init dictionary
headers={}# Loop through lines
forlineintext.split("\n"):# Split field name and value up so they can be assigned
header=line.split(": ")iflen(header)<2:continue# no ": ", so it's probably just an empty line
eliflen(header)>2:print("Help!")# This shouldn't happen
else:headers[header[0]]=header[1]returnheaders
This returns a dictionary with all of the fields and values under Request Headers. Now, let's parse the header and make the request (to submit the Google Form).
Hey Simon, Thank you for your feedback. I'm glad, it helped you. About Error 401, I didn't ran into this, and I think it's because of the url. Have you changed your google form url as mentioned in the post? You've to change the /viewform to /formResponse at the end of the url. I'm not sure if this will prevent the 401 error but I guess, this should work.
And about Form Data, thank you so much about that. Finding attributes from the developer tools is the most tiresome work in this script.
Again, thanks for your comment! And it feels good when someone uses your script and tells you how can you improve it!😄
I found this helpful, Simon. I did some experiments to figure out what parts of the headers were needed and I was able to get it down to only one: The cookie: line.
My concern is that this cookie will eventually expire, so hardcoding it may not be a long term solution.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Great post, James! I needed to do a similar task with google forms, and this was very helpful. However, it didn't work right off the bat for me. After using
requests.post(url, data=d)
, I got the status code401
(which stands for an Unauthorized Error).Side Note: no error is actually thrown. To view the status code, print the object or use
r.status_code
.However, I was able to find a solution, and for the sake of the other readers I wanted to share it here. Here's what I did:
First, I filled out the form using chrome. After submitting the form, I used the developer tools in Chrome (on the landing page of the form) to look at the headers. That can be found on the Network tab after selecting the
formResponse
entry. For anyone following along, there should be a list of several header categories including General, Response Headers, Request Headers, and Form Data.Another Side Note: all of the field names with the data you entered in the Google Form can be found under Form Data; I found that using this was a lot easier than hunting through html tag attributes.
Anyways, Request Headers is the one you want. Now, I'm not sure if you need every single field under the Request Headers, but I decided to use all of them. To do this, I just copied the plain text under Request Headers and assigned it to a variable like this:
Then, I parsed the string using this function:
This returns a dictionary with all of the fields and values under Request Headers. Now, let's parse the header and make the request (to submit the Google Form).
I hope this helps!
Hey Simon, Thank you for your feedback. I'm glad, it helped you. About Error 401, I didn't ran into this, and I think it's because of the url. Have you changed your google form url as mentioned in the post? You've to change the
/viewform
to/formResponse
at the end of the url. I'm not sure if this will prevent the 401 error but I guess, this should work.And about Form Data, thank you so much about that. Finding attributes from the developer tools is the most tiresome work in this script.
Again, thanks for your comment! And it feels good when someone uses your script and tells you how can you improve it!😄
I did use
/formResponse
. However, I didn't make the form, so there might be some setting differences... I'm not sure. Thanks again, though.how i can get the header text please?
Here's a screenshot:
It's under Network, click the
fromResponse
request, then make sure you're on the header tab and look for "Response Headers."Thanks a lot
I found this helpful, Simon. I did some experiments to figure out what parts of the headers were needed and I was able to get it down to only one: The
cookie:
line.My concern is that this cookie will eventually expire, so hardcoding it may not be a long term solution.