我试图使用POST请求上传一个文件,但文件没有被上传。

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

这个API URL在swagger和postman中可以使用

* 设置 * 图书馆申请图书馆馆藏

*** Variables ***
${base_URL}=    https://someurl.com

${file_name}=   Get Binary File   Test.png

*** Test Cases ***
TC008_Upload_File

    create session      upload_file     ${base_url}
    ${body}=    create dictionary      asset=${file_name}
    ${header}=  create dictionary      Content-Type=multipart/form-data:boundary= <calculated when request is sent> Accept=text/plain

    ${response}=    post request   upload_file  /api/insertFile  data=${body}  headers=${header}
    log to console      ${response.content}
      #to validate status code with the actual status code
     ${Status_code}=    convert to string   ${response.status_code}
     should be equal    ${Status_code}      200

     #to validate the content of the response body
     ${response_body}=  convert to string   ${response.content}
     should contain     ${response_body}    true

我的Curl URL是:

curl -X POST "https://someurl.com/api/insertFile" -H "accept: text/plain" -H "Content-Type: multipart/form-data" -F "[email protected];type=image/png"
python api file-upload robotframework web-api-testing
1个回答
0
投票

很明显,你想把一个文件的内容作为请求的主体,但是--你是想在变量部分获取它。

*** Variables ***
${base_URL}=    https://someurl.com

${file_name}=   Get Binary File   Test.png     # <-- this call here

在那个地方--变量部分--你不能运行关键字并将它们的返回值分配给一个变量;所有放在那里的东西都会直接变成值(即使它类似于关键字的名称)。因此 ${file_name}'的值实际上是 "Get Binary File C:\Users\fl792/PycharmProjects\ResrAssured/VideoGameProject/Test15.txt",而不是该文件的内容。

你可以通过把它移到一个可调用的地方来解决这个问题--比如测试用例本身。


*** Test Cases ***
TC008_Upload_File
    ${file_name}=   Get Binary File   Test.png
© www.soinside.com 2019 - 2024. All rights reserved.