【问题标题】:Python API calls multipart/form-dataPython API 调用 multipart/form-data
【发布时间】:2015-12-23 21:41:42
【问题描述】:

我正在尝试使用 Python 的 --form data 转换 curl 调用

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

我得到的返回是所有提交样本的默认列表。所以我确信它没有得到所有的标题数据。

我可以使用传递多个表单数据标题的示例。

谢谢。

【问题讨论】:

  • 请显示你的输出。
  • multipart/form-data 不是这样工作的。试试requests module

标签: python api curl


【解决方案1】:

使用 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

【讨论】:

    猜你喜欢
    • 2015-09-19
    • 1970-01-01
    • 2016-01-29
    • 2022-06-11
    • 1970-01-01
    • 2023-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多