Reputation: 25
So I created a google form, here's the link : https://docs.google.com/forms/d/e/1FAIpQLSeI3-jkg0oedRXECibaxbGZiFyOHGYjvNiOXcADBK9Qq__gUg/viewform
I want to answer with Python from Python, I tried but it returns 400, bad request.
This is my code :
import requests
def f():
url = 'https://docs.google.com/forms/d/e/1FAIpQLSedKV-pzCYZjnp-tAm6Ww9HMoosYPucSL2y9usEIz6yBizbFg/formResponse'
form_data = {"entry.386860893":"Python","fvv":1,"draftResponse":'[]',"pageHistory":0,"fbzx":-6718008993703688486}
user_agent = {'Refer':'https://docs.google.com/forms/d/e/1FAIpQLSeI3-jkg0oedRXECibaxbGZiFyOHGYjvNiOXcADBK9Qq__gUg/viewform?fbzx=-6860115379139697000','User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36'}
r = requests.post(url, data=form_data, headers=user_agent)
print(r)
Thanks!
Upvotes: 2
Views: 6776
Reputation: 347
I've just solved it by disabling this "Response receipts" option.
Upvotes: 0
Reputation: 2061
A little late but, I found out that one of the possible reason why you are getting a response error of 400 is because when using the requests
module, the posted data is automatically url encoded. Hence posting options that contains characters such as "+", "@" ..etc will cause a bad form error (google form replaces whitespaces by +).
Hence, one way you can work around this is to have the request post http URL instead by passing a string.
url = "https://docs.google.com/forms/xxx/formResponse"
string = "entry.xxx=Option+1&entry.xxx=option2"
r = requests.post(url, params = string)
Hope this helps.
Upvotes: 0
Reputation: 794
I've been wrong. The error is not caused by missing or invalid data of your session. The code example below (the use of session) is not required! I just changed the URL from your code example to the link from your question and it worked for me:
import requests
url = 'https://docs.google.com/forms/d/e/1FAIpQLSeI3-jkg0oedRXECibaxbGZiFyOHGYjvNiOXcADBK9Qq__gUg/formResponse'
form_data = {"entry.386860893":"Python","fvv":1,"draftResponse":'[]',"pageHistory":0,"fbzx":-6718008993703688486}
requests.post(url, data=form_data) # response [200]
Probably you need to fetch a token or something first. For interactive communication with a website I'd recommend using requests.Session()
to handle interactions with a webpage.
http://docs.python-requests.org/en/master/user/advanced/
If I use the link from your code example I get bad request error as well, but using the link from your question this works:
url1 = 'https://docs.google.com/forms/d/e/1FAIpQLSeI3-jkg0oedRXECibaxbGZiFyOHGYjvNiOXcADBK9Qq__gUg/viewform'
url2 = 'https://docs.google.com/forms/d/e/1FAIpQLSeI3-jkg0oedRXECibaxbGZiFyOHGYjvNiOXcADBK9Qq__gUg/formResponse'
form_data = {"entry.386860893":"Python","fvv":1,"draftResponse":'[]',"pageHistory":0,"fbzx":-6718008993703688486}
s = requests.Session()
s.get(url)
r = s.post(url2, data=form_data)
print(r)
Upvotes: 2