python POST with file - convert from RAW data -


i have been trying thing work days, no luck. have worked through majority of issues, @ new error , don't know why.

i have read multiple posts, , asked few questions along way, stumped , seems nothing search helps.

here scenario:

  • i need log website (let's call site_a) , pull report
  • next, need log separate website (site_b) can upload report
  • with site_b, required log in, session id, use session id upload file

this done api https post calls... (yes, api requires post everything).

site_a works - log in , pull data, gives me xml file. file converted csv file, due site_b accepting .csv.

site_b however, giving me errors.

using burp, able intercept raw data of test api published company. raw data looks (some data removed):

post /api/import http/1.1 host: hosturl accept: application/json, text/javascript, */*; q=0.01 accept-language: en-us,en;q=0.5 accept-encoding: gzip, deflate, br x-requested-with: xmlhttprequest content-type: multipart/form-data; boundary=---------------------------28791504620783 cookie: asc_session_id=sessiontoken; skipbrowsercheck=true; jsessionid=sessionstuff; asp.net_sessionid=aspsession connection: close  -----------------------------28791504620783 content-disposition: form-data; name="token"  sessiontoken -----------------------------28791504620783 content-disposition: form-data; name="name"  wh -----------------------------28791504620783 content-disposition: form-data; name="uploadedfile"; filename="data.csv" content-type: application/vnd.ms-excel  data,data,data,data data,data,data,data  -----------------------------28791504620783-- 

i have converted following python program:

# login - session token app = requests.session() loginresponse = app.post(loginurl, headers=headers, data=json.dumps(post_body), verify=false)  if loginresponse.status_code != 200:     print ('login attempt failed. please try again later.')  else:     userdata = json.loads(loginresponse.text)     sessiontoken = userdata["sessionid"]     print ('login successful! attempting upload file...')      # try upload file     uploadurl = 'uploadurl/'      headers = {         'token': sessiontoken,         'name': 'wh',         'post': '/api/import/285/2 http/1.1',         'accept': 'application/json, text/javascript, */*; q=0.01',         'accept-language': 'en-us,en;q=0.5',         'accept-encoding': 'gzip, deflate, br',         'connection': 'close'        }         files = {'data.csv': open('data.csv', newline='')}      uploadresponse = app.post(uploadurl, headers=headers, files=files, verify=false)      if uploadresponse.status_code != 200:         print ('status report: %i' % uploadresponse.status_code)      else:          print ('upload complete') 

i have tried move things around bit, , place somethings in body rather header. gives me errors either logging in, or incorrect file type. have established need use token , name in header. not sure if have use "files" variable in python requests (files='whatever')

but point, 500 error.

can please point me in right direction?

i have read following posts, , while have helped little, aren't helping here: post 1, post 2, post 3

note: have not confirmed problem isn't on server. may not code @ all. seems since api test code on website works, should work in python well.

i not sure if it's api, or if messing up, in case had create body looked similar raw data. under impression "files=" me, apparently did not.

i defined boundary in multipart/form , used add sections.

the end result looked like:

headers = {             'content-type': 'multipart/form-data; boundary=159753',             'asc_xsrf_token': sessiontoken,         }             open('data.csv', newline='') csvfile:             post_body = (                 "\n--159753\n"                 "content-disposition: form-data; name=\"asc_xsrf_token\"\n\n"                 + sessiontoken +                  "\n--159753\n"                 "content-disposition: form-data; name=\"name\"\n\n"                 "wh\n"                 "--159753\n"                 "content-disposition: form-data; name=\"uploadedfile\"; filename=\"data.csv\"\n"                 "content-type: application/vnd.ms-excel\n\n"                 + csvfile.read() +                  "\n--159753--"                 )             csvfile.close() 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -