Home > Mobile >  How to rest API with PUT method via robot framework
How to rest API with PUT method via robot framework

Time:11-29

I have a Test Suite

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    [email protected]    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}

The returned result is Login pass, UpdateLanguageStatus failed. UpdateLanguageStatus return 400. I don't know the reason. Another side, I run this test case on postman, it work wel enter image description here. Do anyone help me?

CodePudding user response:

It looks like the dictionary content of ${body} is not serialized to json before sending the request. The output looks like this:

{'languageId': '20'}

It should look like this:

{"languageId": "20"}

You can add the following before sending:

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

Additional note - if languageId is int then you may need to change the line you already have so the 20 is within ${} e.g.

${body}=    Create Dictionary    languageId=${20}
  • Related