javascript - How to post files to a multipart/form-data? -


i want upload , submit files form on http://www.broadinstitute.org/cmap/newquery?servletaction=querysetup&querytype=quick (need log-in). form looks follows

<form name="form1" enctype="multipart/form-data" method="post" action="newquery">     <input type="hidden" name="servletaction" value="quickquery">         <div class="formtable">             <div class="row">                 <span class="label" style="width: 100px;">up tag file:</span>                 <span class="field"><input type="file" name="ups" size="30" accept="grp"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font size="-1"><a href="#" onclick="window.open('help_topics_frames.jsp?topic=tag list', 'helptopicswindow', 'scrollbars,resizable,height=600,width=700')">tag file help</a></font></span>             </div>             <div class="row">                 <span class="label" style="width: 100px;">down tag file:</span>                 <span class="field"><input type="file" name="dns" size="30" accept="grp"></span>             </div>             <div class="row">                 <span class="label" style="width: 100px;">&nbsp;</span>                 <span class="navigation"><input type="button" onclick="submitform()" name="submitbutton" value="execute query"></span>             </div>         </div> </form> 

first, answer https://stackoverflow.com/a/22547541/651779 cookie log-in credential

import http.cookiejar import urllib bs4 import beautifulsoup import requests  submit_signature_url = 'http://www.broadinstitute.org/cmap/newquery?servletaction=querysetup&querytype=quick' login_url = 'http://www.broadinstitute.org/cmap/j_security_check'  login_values = urllib.parse.urlencode({'j_username': 'example',                                        'j_password': 'example',                                         'submit': 'sign in'})  payload_submit_signature = bytes(login_values, 'ascii')  cj = http.cookiejar.cookiejar() opener = urllib.request.build_opener(     urllib.request.httpredirecthandler(),     urllib.request.httphandler(debuglevel=0),     urllib.request.httpshandler(debuglevel=0),     urllib.request.httpcookieprocessor(cj))  resp_1 = opener.open(submit_signature_url) #first call capture jsessionid resp = opener.open(login_url, payload_submit_signature) 

this works correctly. want post files form. tried using answer https://stackoverflow.com/a/12385661/651779

# changed after brett lempereur's answer values = {'ups':open(r'path\to\up.grp','rb'),           'dns':open(r'path\to\down.grp','rb')}  submit_signature_url = 'http://www.broadinstitute.org/cmap/newquery?servletaction=querysetup&querytype=quick' req = requests.post(submit_signature_url, files=values, cookies=cj) soup = beautifulsoup(req.text) print(soup.prettify()) 

this prints

requested url:  http://www.broadinstitute.org/cmap/newquery java.lang.nullpointerexception 

if logged in , go http://www.broadinstitute.org/cmap/newquery in browser same. if instead use data instead of files in requests.post: req = requests.post(submit_signature_url, data=values, cookies=cj), prints html of page containing form, did not post form.

how can post multipart/form-data?


for example up-file, copy following file file , call up.grp

205258_at 221369_at 205751_at 212954_at 219914_at 206703_at 207352_s_at 203548_s_at 203549_s_at 210382_at 212110_at 213805_at 213935_at 218739_at 219737_s_at 219738_s_at 204131_s_at 204132_s_at 210655_s_at 217399_s_at 206791_s_at 206792_x_at 211818_s_at 221523_s_at 221524_s_at 

and example down file, copy following in file , call down.grp

204725_s_at 211063_s_at 219921_s_at 200618_at 219701_at 220342_x_at 220926_s_at 201323_at 204692_at 221956_at 222017_x_at 90610_at 217755_at 207843_x_at 209366_x_at 215726_s_at 201827_at 201185_at 212411_at 201692_at 214484_s_at 202910_s_at 202381_at 200663_at 201459_at 219770_at 220853_at 201349_at 207015_s_at 207016_s_at 212338_at 220330_s_at 

to hazard guess, current version of requests module expects parameter have 1 of following formats:

files = {"name": file-handle, ...} files = {"name": (file-name, file-handle, content-type,), ...} 

have tried replacing creation of files dictionary along lines of:

values = {'ups': open(r'path\to\up.grp', 'rb'), 'dns': open(r'path\to\down.grp', 'rb')} 

your code technically not semantically correct in some file data sent site, contents of string literal r'path\to\up.grp'.

from reading form, should pass dictionary {"servletaction": "quickquery"} data parameter requests.post call. in addition, url post should http://www.broadinstitute.org/cmap/newquery, without current query string you've used in original code.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -