Robot Framework,如何仅使用请求 API 下载响应体中返回的文件。

huangapple go评论51阅读模式
英文:

Robot Framework, How to download a file returned in a response body using only request api

问题

我正在使用机器人框架发送请求而不打开浏览器。其中一个请求应该下载一个文件。但是,当我发送这个请求时,文件没有被下载(即使我有一个200状态码)。

测试在状态码为200时通过了。但是没有下载文件。

以下是响应头:

  • content-disposition: attachment;
  • filename="Export Site Parameter_2020-01-03_09-21-39.xlsx";
  • filename*=UTF-8''Export%20Site%20Parameter_2020-01-03_09-21-39.xlsx
  • content-length: 3767
  • content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
  • date: Fri, 03 Jan 2020 09:21:38 GMT
  • server: Kestrel
  • x-powered-by: ASP.NET

感谢您的帮助。

英文:

I'm using robot framework to send requests without opening a browser. One of the requests is supposed to download a file. But when I send this request the file is not downloaded (even if I have a 200 status code).

${uri}    set variable    /api/pricing/ExportImportParams/downloadExportParams
Create Session    test    ${url}    cookies=&{cookies}
${resp}=    Get Request    test    ${uri}    headers=&{headers}
${resp_code} =    Set Variable    ${resp.status_code}
${resp_code} =    Convert To String    ${resp_code}
Run Keyword And Continue On Failure    Should be Equal    ${resp_code}    200

The test is passed while the status code is 200.
But no file is downloaded

Here is the response Headers

> content-disposition: attachment;
> filename="Export Site Parameter_2020-01-03_09-21-39.xlsx";
> filename*=UTF-8''Export%20Site%20Parameter_2020-01-03_09-21-39.xlsx
> content-length: 3767 content-type:
> application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
> date: Fri, 03 Jan 2020 09:21:38 GMT server: Kestrel x-powered-by:
> ASP.NET

Thanks for helping

答案1

得分: 2

假设您正在使用RequestsLibrary执行HTTP Get请求以检索文件。尽管文档没有明确指定,但返回的响应对象的content属性包含数据。然后可以使用标准的OperatingSystem库将其轻松存储在文件中。

*** Settings ***
Library    RequestsLibrary
Library    OperatingSystem

*** Test Cases ***
File Download Using RequestsLibrary
    ${file_name}      Set Variable    file_example_XLS_10.xls
    ${uri}            Set Variable    /wp-content/uploads/2017/02/${file_name}

    Create Session    test    https://file-examples.com
    ${response}=      Get Request    test    ${uri}

    Run Keyword And Continue On Failure    Should Be Equal As Numbers     ${response.status_code}    200

    Create Binary File     ${EXECDIR}/${file_name}     ${response.content}

请注意,这是给定的代码部分的翻译。

英文:

Making an assumption that you are using the RequestsLibrary to perform the HTTP Get request to retrieve the file. Althought the documentation does not specify it, the content attribute of the returned response object contains the data. This can then be easily stored in a file using the standard OperatingSystem library.

*** Settings ***
Library    RequestsLibrary
Library    OperatingSystem

*** Test Cases ***
File Download Using RequestsLibrary
    
    
	${file_name}      Set Variable    file_example_XLS_10.xls
	${uri}            Set Variable    /wp-content/uploads/2017/02/${file_name}

	Create Session    test    https://file-examples.com
	${response}=          Get Request    test    ${uri}

	Run Keyword And Continue On Failure    Should Be Equal As Numbers     ${response.status_code}    200

    Create Binary File     ${EXECDIR}/${file_name}     ${response.content} 

huangapple
  • 本文由 发表于 2020年1月3日 17:41:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576193.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定