发布带有json数据的多个文件时出错

时间:2019-04-04 18:55:02

标签: python api python-requests

我有API可以接受带有json数据的多个文件-

curl -X POST \
  http://localhost:25965/v1/import \
  -H 'Content-Type: application/json' \
  -H 'Postman-Token: xxxxxxxx-xxxx-xxxx-xxxx-ba66a9b8d6cb' \
  -H 'cache-control: no-cache' \
  -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
  -F 'files=@C:\Users\user\File1.xlsx' \
  -F 'files=@C:\Users\user\File2.xlsx' \
  -F 'files=@C:\Users\user\File3.xlsx' \
  -F 'values=[
   {
        "field1": "Value11",
        "field2": "Value21",
        "field3": "File1.xlsx"
    },
   {
        "field1": "Value21",
        "field2": "Value22",
        "field3": "File2.xlsx"
    },
   {
        "field1": "Value31",
        "field2": "Value32",
        "field3": "File3.xlsx"
    }
]'

我正在尝试将此请求转换为python。 收到请求后,我收到错误无法解包的值太多(预期为2)

def PostFiles(self, apiName, values, files):
        url = self.ApiUrl + apiName 
        params = {}
        for file in files:
            params.update({'files':file})
        response = requests.post(url, 
                                 files=params,
                                 data=values,
                                 headers={
                                     'Content-Type':'application/json'},
                                 auth=HTTPKerberosAuth(delegate=True))
        return response

我也尝试了request_toolbelt,但是没有用。

def PostFiles(self, apiName, values, files):
        url = self.ApiUrl + apiName  
        params = {}
        for file in files:
            params.update({'files':file})
        params.update({'values':json.dumps(values)})

        multipart_data = MultipartEncoder(params)

        headers = { 'Content-Type':multipart_data.content_type}
        response = requests.post(url, 
                                 data=multipart_data,
                                 headers=headers,
                                 auth=HTTPKerberosAuth(delegate=True))
        return response

这是我的函数调用-

 files = [open(join(directory, filename), 'rb')]
    #files = [('files',(filename, open(join(directory, filename), 'rb'),'application/vnd.ms-excel'))]
    values = [{
             'field1': 'value11',
             'field2' : 'value21',
             'field3' : 'File1.xlsx'
        }]
    response = PostFiles('import', values, files)
    print(response)

有一些与此主题相关的帖子,但是我找不到任何带有json的多个文件。

1 个答案:

答案 0 :(得分:0)

我按照Post JSON and file in single request修改了代码,并按预期工作。

def PostFiles(self, apiName, values, files):
        url = self.ApiUrl + apiName 
        params = {
            'values': (None, json.dumps(values), 'application/json')
            }
        for file in files:
            params.update({'files':file})
        response = requests.post(url, 
                                 files=params,
                                 auth=HTTPKerberosAuth(delegate=True))
        result = response.json()
        if response.status_code != 200 :
            raise Exception(result)
        return json.dumps(result)