Robotframework.request-如何发出内容为“ multipart / form-data”和键值的POST请求

时间:2019-01-25 11:51:48

标签: java python python-requests robotframework

我想使用HttpRequestLibrary在Robot Framework中使用“ Content-Type:multipart / form-data”发出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变量。它确实可以在Postman中正常工作,因为在请求的正文中,我可以选择表单数据,然后将键定义为jsonBody,并将其值定义为

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

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

${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"

有人可以帮我这个忙吗,或者丢下有关如何将HttpRequestLibrary与Content-Type一起使用的正确示例:multipart / form-data

2 个答案:

答案 0 :(得分:0)

如果您尝试获取此json:

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

您的字典语法不正确

更改:

${data}=  Create Dictionary  jsonBody={'method' = 'BASIC_AUTH', 'username' = 'xxx', 'password' = 'xxxxx'}

给出结果:

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

TO:

${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}机器人框架,将jsonBody作为字典的第一个键来创建嵌套字典,您需要的是在字典前的字符串。

您可以使用catanate将字符串连接在一起

*** 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'}

答案 1 :(得分:0)

经过多次尝试,我来到了这种情况,但同样失败了:

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}

结果是:

"message": "Multipart request resolution failed. Please ensure that each part is properly formatted."

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