Python API调用multipart/form-data [英] Python API calls multipart/form-data

查看:383
本文介绍了Python API调用multipart/form-data的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用--form数据转换Python的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

我尝试过的事情:

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.

谢谢.

推荐答案

使用unirest的示例调用

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

您可以查看此博客以获取更多详细信息 http://stackandqueue.com/?p=57

You can check this blog for more details http://stackandqueue.com/?p=57

这篇关于Python API调用multipart/form-data的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆