RequestsLibrary - 如何在同一 POST 请求中发送数据和文件

问题描述 投票:0回答:2

我有以下 Python 脚本,用于上传

file
以及一些
payload
。我尝试在机器人框架上运行,但收到的响应输出为
Bad request

Python 脚本

import requests

url = "https://api.company.net/certificates/upload/0b36f50c-8458-4478-b83e-cfb99wef9c73"

payload={'flowChunkNumber': '1',
         'flowChunkSize': '1048576',
         'flowCurrentChunkSize': '25020',
         'flowTotalSize': '25020',
         'flowIdentifier': '25020-test_certificate_reportpdf',
         'flowFilename': 'test_certificate_report.pdf',
         'flowRelativePath': 'test_certificate_report.pdf',
         'flowTotalChunks': '1'}
files=[
  ('file',('test_certificate_report.pdf',open('/C:/RobotFramework/DATA_Test/support_files/test_certificate_report.pdf','rb'),'application/pdf'))
]
headers = {
            'X-Requested-With': 'XMLHttpRequest',
            'Cookie': 'PHPSESSID=7ios0o98lbljrdrlvfk0456rujh'
          }
response = requests.request("POST", url, headers=headers, data=payload, files=files)

print(response.text)

以下是我编写的

Robot Framework
测试脚本,它给出了错误-

TEST_案例流程

*** Variables ***        
        &{headers}    Create Dictionary    X-Requested-With=XMLHttpRequest
        ${email_admin}   [email protected]
        ${password}    password123
        ${base_url}    https://api.company.net

*** Test Cases ***
 Login
        &{auth_dict}=    Create Dictionary    email    ${email}    password    ${password}
        Create Session    api    ${base_url}    verify=True
        ${test_uri}    Set Variable    /session/login
        ${response}    POST On Session    api    ${test_uri}    json=${auth_dict}    headers=&{headers}

  Upload_File
        &{payload}    Create Dictionary    flowChunkNumber    1    flowChunkSize    1048576    flowCurrentChunkSize    25020    flowTotalSize    25020    flowIdentifier    25020-test_certificate_reportpdf    flowFilename    test_certificate_report.pdf    flowRelativePath    test_certificate_report.pdf    flowTotalChunks 1
        &{file}    Create Dictionary    file=${file_location}/test_certificate_report.pdf
        ${test_uri}    Set Variable    https://api.company.net/certificates/upload/0b36f50c-8458-4478-b83e-cfb99wef9c73
        ${response}    POST On Session    api    ${test_uri}    data=&{payload}    files=&{file}    headers=&{headers}

HTTPError: 400
客户端错误:URL 请求错误: https://api.company.net/certificates/upload/0b36f50c-8458-4478-b83e-cfb99wef9c73

有没有更好的方法在Robot Framework中的同一个POST请求中同时传递

data
file

提前致谢。

---更新--

上传的文件是用于测试的空白pdf文件。不应从文件中读取任何数据。

python python-3.x python-requests automated-tests robotframework
2个回答
0
投票

用此替换第三行

${file}=    Get File For Streaming Upload    ${file_location}/test_certificate_report.pdf

将文件作为

data
的值和 JSON 负载作为
json
传递。

${response}    POST On Session    api    ${test_uri}    json=${payload}    data=${file}    headers=&{headers}

0
投票
${file_pdf}         app/file/file_upload.pdf

Send Post Request With Upload File
    [Arguments]  ${url}     ${payload}   ${files}    ${token}=${token_admin}
    ${header}=  create dictionary   Authorization=Bearer ${token}
    Create Session      test     ${base_url}
    ${response}=    POST On Session    test    ${url}    headers=${header}     files=${files}      data=${payload}
    [Return]   ${response}

Test Upload file Success
    ${json_data}=    Evaluate    str(${json_data).replace("'",'"')
    ${file_data}  Get File For Streaming Upload    ${file_pdf}
    ${files}  Create Dictionary  files=${file_data}
    ${data}=    Create Dictionary    req_body=${json_data}
    ${response}=    Send Post Request With Upload File      ${endpoint}     ${data}       ${files}

Send Post Request With Upload File 是发送数据的关键字, 当使用获取文件进行流式上传时,我们不需要Content-type

它将对应下面的python请求:

url = "http://localhost/api/xxx"

payload = {'req_body': '{"name":"xxx","password":"1111"}
files=[
  ('files',('file_upload.pdf',open('app/file/file_upload.pdf','rb'),'application/pdf'))
]
headers = {
  'Authorization': 'Bearer eyJ....'
}

response = requests.request("POST", url, headers=headers, data=payload, files=files)
© www.soinside.com 2019 - 2024. All rights reserved.