Robotframework.request如何使用内容“multipart/formdata”和一个键值发出POST请求

2024-09-29 00:12:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用HttpRequestLibrary在Robot框架中使用“Content-Type:multipart/formdata”发出POST请求,但它不适合我。在

请求的卷曲度为:

curl -X POST "https://xxx-approuter-xxx-xxxxxx" -H  "accept: application/json" -H  "Content-Type: multipart/form-data" -F "pkcsFile=" -F "crtFile=" -F "privateKey=" -F "jsonBody={'method' = 'BASIC_AUTH', 'username' = 'xxx', 'password' = 'xxxxx'}"

我的问题是我不知道jsonBody变量应该在哪里以及如何用robot scripr定义。它在Postman中工作正常,因为在请求的body中,我可以选择formdata,然后将key定义为jsonBody,其值为

^{pr2}$

下面是我的机器人脚本示例:

${headers}=    Create Dictionary    Content-Type=multipart/form-data  Authorization=${token}
${data}=  Create Dictionary  jsonBody={'method' = 'BASIC_AUTH', 'username' = 'xxx', 'password' = 'xxxxx'}
${resp}=    Post Request   my_session   /authentications  data=${data}  headers=${headers}

返回错误:

"message": "Required AuthenticationDto parameter jsonBody is not present"

有人能帮我吗?或者举一个关于如何使用内容类型为multipart/formdata的HttpRequestLibrary的适当例子


Tags: formauthdatabasictypeusernamecontentpost
2条回答

经过多次尝试,我想到了这个场景,但也失败了:

Creates an authentication schema
    [Arguments]   ${providerType}  ${providerMode}  ${pkcsFile}  ${returnStatus}
    ${headers}=    Create Dictionary    Content-Type=multipart/form-data  Authorization=${jwt_token}
    ${data}=  Create Dictionary   method=BASIC_AUTH  username=xxx  password=xxx
    ${formData}=  Create Dictionary   jsonBody=${data}
    Log  ${formData}
    ${resp}=    Post Request   httpbin    /configuration/test/test/tests  data=${formData}  headers=${headers}
    Log  ${resp}
    Should Be Equal As Strings    ${resp.status_code}    ${returnStatus}

结果是:

^{pr2}$

我必须以某种方式提供键值jsonBody,但我不确定是否正确

如果要获取此json:

{'method' = 'BASIC_AUTH', 'username' = 'xxx', 'password' = 'xxxxx'}

你的字典语法不正确

更改:

^{pr2}$

结果是:

{'jsonBody': "{'method' = 'BASIC_AUTH', 'username' = 'xxx', 'password' = 'xxxxx'}"}

收件人:

${data}=  Create Dictionary   method=BASIC_AUTH  username=xxx  password=xxx

结果就出来了

{'method' = 'BASIC_AUTH', 'username' = 'xxx', 'password' = 'xxxxx'}

编辑:

如果你需要得到这根绳子

jsonBody={'method' = 'BASIC_AUTH', 'username' = 'xxx', 'password' = 'xxxxx'}

您要做的是,您正在使用jsonBody=${var}robot框架将把jsonBody作为字典的第一个键来创建一个嵌套字典您需要的是一个字符串,然后再进行听写。在

你可以用卡塔特把绳子连在一起

*** Test Cases ***
Test
        ${jsonBody} =  Set variable  jsonBody
        ${data}=  Create Dictionary   method=BASIC_AUTH  username=xxx  password=xxx
        ${finishedbody} =  Catenate   ${jsonBody}=${data}
        log  ${finishedbody}

结果

${finishedbody} =    jsonBody={'method': 'BASIC_AUTH', 'username': 'xxx', 'password': 'xxx'}

相关问题 更多 >