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)
Community
  • 1
  • 1
J.Blake
  • 3
  • 1
  • 3
  • Is this your form, can I submit test data to it? – Bamcclur Jul 08 '16 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. – J.Blake Jul 08 '16 at 15:37

1 Answers1

2

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       
Bamcclur
  • 1,949
  • 2
  • 15
  • 19
  • I tested this against my own form at https://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')} – Bamcclur Jul 07 '16 at 22:29
  • 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 Jul 08 '16 at 15:28
  • @J.Blake have you tried using my form data, with fewer fields? That should be able to test your firewall. – Bamcclur Jul 08 '16 at 15:38
  • Yeah it is not uploading to that form either. – J.Blake Jul 08 '16 at 15:44
  • @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. – Bamcclur Jul 08 '16 at 15:47
  • I received a file from the test form I have out there. Perhaps if you use actual data instead of "Some Company", "Some Email" it will work. As I don't intend to purchase anything, I don't want to send my information to this exact form, the response I am getting from the url you included in the question is along the lines of data validation. – Bamcclur Jul 08 '16 at 16:13
  • I was able to submit the form! I got the form submitted successfully message! Now I play the waiting game until i get their confirmation email. Thanks for all your Help! – J.Blake Jul 08 '16 at 16:45
  • Is there a field missing in my answer, or is it value related? – Bamcclur Jul 08 '16 at 16:48
  • There was some fields missing to some hidden fields i didn't have in my original post – J.Blake Jul 08 '16 at 17:53