0

Trying to write a script that fills in an online form at this website and uploads a zip file. I have looked at the documentation and several other posts on here but still cant get my script to upload the file.

Here is the html source for the file upload:

<input type="file" id="field19567427" name="field19567427"
size="30" class="fsField fsUpload uploadTypes-jpg,jpeg,gif,png,bmp,tif,
doc,docx,xls,xlsx,txt,mp3,mp4,aac,wav,au,wmv,avi,mpg,mpeg,zip,gz,rar,z,tgz,tar,sitx" />

Here is my python code(forgive all my imports I have been trying a lot of different approaches):

import urllib
import urllib2
import cookielib
import webbrowser
import os
import base64
import requests
from pprint import pprint

walla = "X:\\Test\\Test.html"
my_file = open("X:\\Some_Directory\\Meh.zip", 'rb')
values = {
    "field19567029" : "Some Company",
    "field20044433" : "Some Email",
    "field40168419" : "Some Phone Num",
    "field19567035" : "Some Code",
    "field19567303" : "Some Distance",
    "field19567306" : "Map Projection",

   }
zippy = {
   "field19567427" :  my_file
    }

url = "http://www.formstack.com/forms/?1455656-XG7ryB28LE"
url2 = "http://httpbin.org/post"
if os.path.exists(walla):
  os.remove(walla)
r = requests.post(url, data=values, files=zippy)
#r.status_code
#pprint(r.json()['headers'])
with open(walla, "w") as f:
    f.write(r.content)
2
  • Is this your form, can I submit test data to it? Commented Jul 8, 2016 at 15:28
  • @Bamcclur its not my form but I suppose you could submit test data to it that was my plan. I also wasn't able to upload a file to your test form either though. Commented Jul 8, 2016 at 15:37

1 Answer 1

3

With your specific url, you need to add some data:

url = "http://www.formstack.com/forms/?1455656-XG7ryB28LE"

session = requests.session()
r = session.get(url)  # This can be used to determine form and viewkey values

data = {
    "form": "1455656", # Added
    "viewkey": "XG7ryB28LE", # Added
    "_submit": "1", # Added
    "field19567029" : "Some Company",
    "field20044433" : "Some Email",
    "field40168419" : "Some Phone Num",
    "field19567035" : "Some Code",
    "field19567303" : "Some Distance",
    "field19567306" : "Map Projection",
   }

files = {"field19567427": open("X:\\Some_Directory\\Meh.zip", 'rb')}

r2 = session.post(url, data=data, files=files)
print r2.content       
Sign up to request clarification or add additional context in comments.

9 Comments

I tested this against my own form at none-lvgvq.formstack.com/forms/test with data = {'form': '2408552', 'viewkey': '7e7UZhfqbU', '_submit': '1', 'field43722688-first': 'first_name', 'field43722688-last': 'last_name'} files = {"field43722693": open("meh.zip", 'rb')}
Thanks for the speedy response! However it still does not look like it is uploading my file do you think formstack could be blocking my file upload? Or maybe a firewall on my end isn't allowing a script to do it?
@J.Blake have you tried using my form data, with fewer fields? That should be able to test your firewall.
Yeah it is not uploading to that form either.
@J.Blake what message do you get when you try to send the data? With my form if the first_name and last_name field match an existing value, it will not resubmit.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.