如何获取json响应的未格式化值

问题描述 投票:0回答:1

当我获取“创建用户”调用响应的 ID 并将其记录在“创建用户”上时。

 Create a User Call
    ...
    ${response_body}    Convert String To Json    ${response.content}
    Set Test Variable    ${response_body}
    Log    ${response_body}
    @{created_userId}    Get Value From Json    ${response_body}    id
    Set Global Variable    @{created_userId}
    Log    @{created_userId}

当我登录时,它会获取我想要使用的 id:

Logging of Id that I want to use

Test Case 
    ...
    Then Delete Created Spotnana User    @{created_userId}
Delete a User Call
    [Arguments]    @{created_userId}
    ${response}    Delete Request   uri=test/users/delete/@{created_userId}    headers=${headers}
    ...

但是当我使用它时,它返回一个格式化变量

uri=testendpoint/users/delete/['0745d28c-12b0-463e-abd7-69e19839291e']

我想用的就是这个:

uri=testendpoint/users/delete/0745d28c-12b0-463e-abd7-69e19839291e

我尝试将其设置为列表并从列表中获取它

python python-3.x rest automated-tests robotframework
1个回答
0
投票

简单的 Python 字符串格式化应该可以解决问题:

# assuming you have the json already, you can convert it to string if necessary
json_response = "uri=testendpoint/users/delete/['0745d28c-12b0-463e-abd7-69e19839291e']"

# remove unwanted chars
formatted_uri = json_response.replace('[', '').replace(']', '').replace("'",'')

print(formatted_uri)

结果:

uri=testendpoint/users/delete/0745d28c-12b0-463e-abd7-69e19839291e

© www.soinside.com 2019 - 2024. All rights reserved.