本文介绍了通过REST API使用多部分/表单数据上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试上传4个文件,以通过Robot Framework在REST API调用中用作请求正文.我正在使用请求库来实现此目的.

I'm trying to upload 4 files to be used as a request body in a REST API call through Robot Framework. I'm using the Requests library to achieve this.

我认为我在设置正确的MIME类型/边界时遇到问题,因为如果我使用pybot运行文件,则会抛出此错误:

I think I'm facing a problem with setting the correct MIME type/boundary as this is the error that is thrown if I run the file using pybot:

{u'errorMessage': u"Couldn't find MIME boundary: ------Bound0901"}
  1. 这是设置MIME边界的正确方法吗?
  2. 是否可以像下面给出的代码示例中那样设置自定义MIME边界?还是我需要设置Web应用程序定义的边界?

这是我用来完成此操作的代码:

Here's the code that I'm using to get this done:

Library  |  RequestsLibrary

*** TestCases ***

1. Login
    Create Session  |  host  |  http://10.10.20.20
    &{headers}=  |  Create Dictionary  |  user=scott  |  password=tiger
    ${response}=  |  RequestsLibrary.Get Request  |  host  |  /api/login  |  ${headers}
    &{headers}    Create Dictionary  |  contentType=multipart/form-data;boundary=----Bound0901
    ${file1}=  |  Get Binary File  |  File1.au
    ${file2}=  |  Get Binary File  |  File2.crs
    ${file3}=  |  Get Binary File  |  File3.cst
    ${file4}=  |  Get Binary File  |  File4.des

    ${data}  |  Create Dictionary  |  ----Bound0901Detail={"Name":"APIContent1","isAICC": true,"version": "1.1","availableOffline": false}----Bound0901${file1}----Bound0901${file2}----Bound0901${file3}----Bound0901${file4}----Bound0901

    ${response}=  |  RequestsLibrary.Post Request  |  host  |  /api/contentimport  |  data=${data}  |  headers=${headers}

    Log    ${response.status_code}
    Log    ${response.json()}

推荐答案

实际上,您正在尝试两件事,

Actually you are trying two things,

  1. 上传多个文件
  2. 发送数据/Json以及文件附件

要上传多个文件

创建一个字典,每个文件一个条目,以part name作为键,将file content作为值.

To upload multiple files

Create a dictionary with one entry per file, with the part name as key and file content as value.

*** Settings ***
Library  RequestsLibrary

*** TestCases ***

1. Login
  Create Session  host  http://10.10.20.20
  &{headers}=  Create Dictionary  user=scott  password=tiger
  ${response}=  RequestsLibrary.Get Request  host  /api/login  ${headers}

  ${fileData1}=  Get Binary File  File1.au
  ${fileData2}=  Get Binary File  File2.crs
  ${fileData3}=  Get Binary File  File3.cst
  ${fileData4}=  Get Binary File  File4.des

  &{fileParts}=  Create Dictionary
  Set To Dictionary  ${fileParts}  file1=${fileData1}
  Set To Dictionary  ${fileParts}  file2=${fileData2}
  Set To Dictionary  ${fileParts}  file3=${fileData3}
  Set To Dictionary  ${fileParts}  file4=${fileData4}

  ${response}=  RequestsLibrary.Post Request  host  /api/contentimport  files=${fileParts}  headers=${headers}

  Log  ${response.status_code}
  Log  ${response.json()}

要多部分提交数据

我不得不深入研究Python Requests库以弄清楚该怎么做.要分段发送数据,必须在各个分段中将content-type属性设置为application/json.为此,并设置多部分的filename属性,该输入值必须是包含fileNamefileContentcontentType的列表.

To Submit Data In MultiPart

I had to dig through the Python Requests library to figure out what to do for this.For sending the data in multipart, the content-type attribute has to be set as application/json in the respective multipart.To do that, and also to set filename attribute of the multipart, the entry value has to be a list with fileName, fileContent and contentType.

*** Settings ***
Library  Collections
Library  OperatingSystem
Library  RequestsLibrary

*** TestCases ***
Login And Import
  Create Session  host  http://10.10.20.20
  &{headers}=  Create Dictionary  user=scott  password=tiger
  ${response}=  RequestsLibrary.Get Request  host  /api/login  ${headers}

  &{fileParts}=  Create Dictionary
  Create Multi Part   ${fileParts}  file1  File1.au
  Create Multi Part   ${fileParts}  file2  File2.crs
  Create Multi Part   ${fileParts}  file3  File3.cst
  Create Multi Part   ${fileParts}  file4  File4.des

  ${data}=  Set Variable  {"Name":"APIContent1", "isAICC": true, "version": "1.1", "availableOffline": false}
  Create Multi Part  ${fileParts}  Detail  data.json  contentType=application/json  content=${data}

  ${response}=  RequestsLibrary.Post Request  host  /api/contentimport  files=${fileParts}  headers=${headers}

  Log  ${response.status_code}
  Log  ${response.json()}

*** Keywords ***
Create Multi Part
  [Arguments]  ${addTo}  ${partName}  ${filePath}  ${contentType}=${None}  ${content}=${None}
  ${fileData}=  Run Keyword If  '''${content}''' != '''${None}'''  Set Variable  ${content}
  ...            ELSE  Get Binary File  ${filePath}
  ${fileDir}  ${fileName}=  Split Path  ${filePath}
  ${partData}=  Create List  ${fileName}  ${fileData}  ${contentType}
  Set To Dictionary  ${addTo}  ${partName}=${partData}

结果如下:

POST http://10.10.20.20/api/contentimport HTTP/1.1
Host: 10.10.20.20
Connection: keep-alive
Accept-Encoding: gzip, deflate
Accept: */*
User-Agent: python-requests/2.13.0
user: scott
password: tiger
Content-Length: 761
Content-Type: multipart/form-data; boundary=363f55556da84a4083532ce822b09259

--363f55556da84a4083532ce822b09259
Content-Disposition: form-data; name="file1"; filename="File1.au"

contents of File1
--363f55556da84a4083532ce822b09259
Content-Disposition: form-data; name="file2"; filename="File2.crs"

contents of File2
--363f55556da84a4083532ce822b09259
Content-Disposition: form-data; name="file3"; filename="File3.cst"

contents of File3
--363f55556da84a4083532ce822b09259
Content-Disposition: form-data; name="file4"; filename="File4.des"

contents of File4
--363f55556da84a4083532ce822b09259
Content-Disposition: form-data; name="Detail"; filename="data.json"
Content-Type: application/json

{"Name":"APIContent1", "isAICC": true, "version": "1.1", "availableOffline": false}
--363f55556da84a4083532ce822b09259--

这篇关于通过REST API使用多部分/表单数据上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 15:29