python/ api/ curl

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.

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()

暂无
暂无

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question python requests api - http multipart post with file and form-data Problem with multipart/form-data with Python and an API that uses requests Python API JSON upload file multipart/form-data with boundary Python multipart/form-data Post Request How to extract binary data from multipart/form-data request? (Python) (multipart request base64 encoded by AWS API Gateway) How to upload photos/pictures to Zoom API using Python (multipart/form-data) with JWT? Python multipart/form-data request error : Data must not be a string Python standard library to POST multipart/form-data encoded data post data using python with help of multipart/form-data option Bad request in multipart/form-data Mirakl API request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM