2

I am trying to convert a curl call with --form data for Python

curl -H "X-API-TOKEN:xxxxxxxxxxxxxxxxxxxxxxxxxx" -X POST \
    --form upload=@2015-07-02-Fiesta-EK-malware-payload.exe \
    --form "owner=userName" https://192.168.1.198/rapi/samples/basic

What I have tried:

url = 'https://%s/rapi/samples/basic' % hostname
q = Request(url)
q.add_header('X-API-TOKEN', 'xxxxxxxxxxxxxxxxxxxxxxxxxx')
q.add_header('Content-Type', 'multipart/form-data; upload=@%s' % fileName)
q.add_header('Content-Type', 'multipart/form-data; owner=userName')
a = urlopen(q).read()

The return I get is the default listing of all submitted samples. So I am sure it is not getting all the header data.

I could use an example of passing multiple form data headers.

Thanks.

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Slid3r
  • 323
  • 1
  • 3
  • 11

1 Answers1

0

A sample call using unirest

import unirest
 
# consume async post request
def consumePOSTRequestSync():
 params = {'test1':'param1','test2':'param2'}
 
 # we need to pass a dummy variable which is open method
 # actually unirest does not provide variable to shift between
 # application-x-www-form-urlencoded and
 # multipart/form-data
 params['dummy'] = open('dummy.txt', 'r')
 url = 'http://httpbin.org/post'
 headers = {"Accept": "application/json"}
 # call get service with headers and params
 response = unirest.post(url, headers = headers,params = params)
 print "code:"+ str(response.code)
 print "******************"
 print "headers:"+ str(response.headers)
 print "******************"
 print "body:"+ str(response.body)
 print "******************"
 print "raw_body:"+ str(response.raw_body)
 
# post sync request multipart/form-data
consumePOSTRequestSync()
benAv
  • 35
  • 5
gvir
  • 256
  • 3
  • 4