gpt4 book ai didi

python - 如何在机器人框架中将凭据传递给 RESTinstance POST 请求?

转载 作者:行者123 更新时间:2023-12-01 01:26:43 24 4
gpt4 key购买 nike

Python 代码(工作正常):

 credentials = ("key","token")
verify = False
if not verify:
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

response = requests.post(url, auth=credentials, data=json.dumps(payload), headers={'content-type': 'application/json'}, verify=verify)
status = response.status_code

机器人框架代码:

我想在机器人框架中重复相同的 API 测试,但我不知道如何将凭据传递给 RESTinstance POST 方法

*** Settings ***
Library REST url=https://testhost.com ssl_verify=${verify}

*** Variables ***
header = {"content-type": "application/json"}

*** Test Cases ***
Test Task
POST endpoint=/api/something body=${payload} headers=${header}
Output response status

错误响应状态 - 401

最佳答案

requests post() 方法中的 auth 参数只是 http 基本身份验证的快捷方式。
另一方面是a very simple (hence - basic) one - 名称为“Authorization”的 header ,值为“Basic b64creds”,其中 b64creds 是“user:password”字符串的 Base64 编码形式。

因此流程非常简单 - 对凭据进行编码,并将其添加为 header 。只有一个警告 - python 的 base64 module works with bytes ,其中Robotframework/python3中的字符串是unicode,因此必须对其进行转换。

${user}=    Set Variable    username
${pass}= Set Variable the_password

# this kyword is in the Strings library
${userpass}= Convert To Bytes ${user}:${pass} # this is the combined string will be base64 encode
${userpass}= Evaluate base64.b64encode($userpass) base64

# add the new Authorization header
Set To Dictionary ${headers} Authorization Basic ${userpass}

# and send the request with the header in:
POST endpoint=/api/something body=${payload} headers=${header}

关于python - 如何在机器人框架中将凭据传递给 RESTinstance POST 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53265635/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com