python同时请求post文件、post文件和数据

2024-04-28 06:58:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我对python(以及一般的编码)非常陌生,所以我希望我能很好地阐述我的问题。我设法拼凑出一个应用程序,它连接到我公司的一个软件的API。在

我已经能够验证、获取令牌、将其传递给其他函数以获取并发布到一些API函数。我正在开发一个新的API调用,它发布一个文件。我被难住了。我找到的所有示例都只显示传递文件,但我需要传递文件、数据和auth头。我尝试了很多不同的代码,但是没有任何东西能让我更接近我。在

首先,这个函数是有效的,但是使用不同的API(groups)来发布一个新的组。它不包括任何文件。在

def apiPost(token):
if not token == "":
    status.run_set_text('Running API POST Call', 3)
    headers = { 'Content-Type':'application/json', 'Authorization':'Bearer '+str(token) }
    data = {"data":{ 'id':0, 'customerId':33, 'name':'567tuhj', 'description':'sdfgsdfg'}}
    r = requests.post(api_url+"v1.1/groups/", headers=headers, data=json.dumps(data))
    ***other code removed updating status bars***
    return r
else:
    ***other code removed updating status bars***

我的开发环境只能访问一个客户,但它仍然要求我将customerId发布到post。在

我已经尝试了数百种不同的转换方式,从我在请求网站教程和其他一些stackoverflow问题上读到的内容,转换成发布一个文件。这将发布到API包文件。根据我访问的Swagger页面,它说我需要在上传文件时包括ID和customer ID。在

^{pr2}$

Tags: 文件函数tokenapijsondatastatuscode
2条回答

首先,您应该将代码格式化为干净、美观和可读

我试图解决你的问题,但我认为你应该在你的问题中附加一些预期的数据、文件、请求参数。在

def apiPost(token):
    if not token == "":
       status.run_set_text('Running API POST Call', 3)
       headers = { 'Content-Type':'application/json',
                   'Authorization':'Bearer '+str(token) }

       data = {"data":
                  { 'id':0,
                    'customerId':33,
                    'name':'567tuhj',
                    'description':'sdfgsdfg'
                  }
              }

       #The part you are looking for probably
       files = {'name_of_file_field' : open("rb", file)}

       r = requests.post(api_url+"v1.1/groups/", headers=headers, data=json.dumps(data), files = files)
       ***other code removed updating status bars***
       return r
    else:
       ***other code removed updating status bars***

Comment:I get back a 400 (bad request) with that accepted answer..

{"status":"FAILURE",
  "error":[{"code":"5004",
  "name":"General Error","severity":"3","message":"Error Occurred During the operation",
  "details":{"5004":"General Error null"}}]} 
{'Connection': 'keep-alive', 'X-Powered-By': 'Undertow/1', 'Server': 'WildFly/9', 'Content-Length': '172', 'Content-Type': 'application/json;charset=UTF-8', 'X-Application-Context': 'application:9090', 'Date': 'Fri, 28 Sep 2018 17:57:57 GMT'}

{请编辑您的Python>版本!

尝试了以下操作,使用文本文件而不是图像:

import requests

url = 'http://httpbin.org/anything'
files = {'file': ('helloworld.txt', open('../test/helloworld.txt', 'rb'), 'text/text')}
data = dict(name='barca', country='spain')
r = requests.post(url, files=files, data=data)

# Print my requests.post header, files and data.
r_dict = r.json()
for key in r_dict:
    print('{}:{}'.format(key, r_dict[key]))

Response from http://httpbin.org/anything:

<Response [200]>

My requests.post header, files and data, send back from host.

method:POST
files:{'file': 'Hello World'}
url:http://httpbin.org/anything
form:{'name': 'barca', 'country': 'spain'}
origin:xx.xx.xx.xx
args:{}
headers:{'Content-Length': '369', 
         'Accept': '*/*', 
         'Content-Type': 'multipart/form-data; boundary=bc3c1927f542430f8166e8f3f27f3c72', 
         'Host': 'httpbin.org', 'Connection': 'close', 
         'User-Agent': 'python-requests/2.11.1', 
         'Accept-Encoding': 'gzip, deflate'}
json:None
data:

测试Python:3.4.2-要求:2.11.1

相关问题 更多 >