如何通过机器人框架使用 PUT 方法休息 API

How to rest API with PUT method via robot framework

我有一个测试套件

Library     RequestsLibrary
Library     JSONLibrary
Library   OperatingSystem


*** Variable ***
${base_url}    https://api.sportpartnerxxx.vn/v1
${identity_URL}    https://identity.sportpartnerxxx.vn
${Profile}    https://api.sportpartnerxxx.vn/v1/profile

*** Test Cases ***

Login
    ${body}=    Create Dictionary    client_id=sportpartner-mobile-app    client_secret=ifd-sportpartner-secret-2021-mobile-app    grant_type=password    username=nnbbtd@gmail.com    password=123456
    ${header}=    create dictionary  content_type=application/x-www-form-urlencoded
    ${response}=    Post    ${identity_URL}/connect/token    headers=${header}    data=${body}
    Set Suite Variable    ${token}    Bearer ${response.json()["access_token"]}
    Set Suite Variable    ${refresh_token}    ${response.json()["refresh_token"]}
    Status Should Be    200
    Log To Console    ${token}

UpdateLanguageStatus
    ${body}=    Create Dictionary    languageId=20
    ${header}=    create dictionary    Content-Type=application/json    Authorization=${token}
    ${response}=    PUT    ${Profile}/me/settings    data=${body}    headers=${header}
    Log To Console    ${response.status_code}
    Log To Console    ${response.content}

returned 结果是登录通过,UpdateLanguageStatus 失败。 UpdateLanguageStatus return 400,不知道是什么原因。另一方面,我运行这个邮递员的测试用例,它工作得很好 enter image description here。有人帮帮我吗?

看起来 ${body} 的字典内容在发送请求之前没有序列化为 json。 输出如下所示:

{'languageId': '20'}

它应该是这样的:

{"languageId": "20"}

您可以在发送前添加以下内容:

${body}  Evaluate  json.dumps(${body})  json

附加说明 - 如果 languageId 是 int,那么您可能需要更改已有的行,以便 20 在 ${} 内,例如

${body}=    Create Dictionary    languageId=