首页 > 解决方案 > 如何解决 HTTPError: 400 Client Error: Bad Request for URL inRobot Frame work

问题描述

我知道有许多类似的问题可用,但没有一个有效。有人可以告诉我下面的测试用例是否有任何语法错误

Create Token
    Create Session      testsession        ${baseUrl}       verify=true
    ${body}=    create dictionary    clientId=unittest.cc.client        clientSecret=RyDQ$xxxxxRtv
    ${header}=  create dictionary    Content-Type=application/json
    ${resp}=    POST On Session    testsession    ${reqUri}    json=${body}    headers=${header}    params=${ApiKeyParameter}
    ${source data}=    Evaluate     json.loads("""${resp.content}""")    json
    ${token}=   Set Variable   ${source data['accessToken']}

   #No errors Uptill this much - Bearer token creation was successful after that getting error while using it

    ${header}=   create dictionary       Authorization=${tpre} ${token}     Content-Type=application/json      cookies=ss-id=KF84fFy4txxxxxxxxx76i; ss-pid=StDTDxxxxxxxxxxxxn7r
    ${body}=    get file    API/data.txt
    log to console    ${header}
    ${resp}=    post on session     testsession     /orders     json=${body}    headers=${header}
    log to console    ${resp.status_code}

问题是每次我运行测试时都会收到 400 错误。下面是 POSTMAN 提供的 Python 代码和使用的标头的屏幕截图。现在我不确定如何在我的 python 或机器人框架中获取HOST标头。如果需要任何其他详细信息,请告诉我。在完成获取或发布请求时,我不确定 URL 格式中的标题有没有办法找出来?

import requests
import JSON

url = "https://domain:10001/orders?format=json"

payload = json.dumps({ Can ignore this part
})
headers = {
  'Authorization': 'Bearer xxx',
  'Content-Type': 'application/json',
  'Cookie': 'ss-id=xxx; ss-pid=xxx'
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)

在此处输入图像描述

标签: apipostmanrobotframework

解决方案


如果你们都遇到过此类问题,请不要忘记检查您发送的 JSON / XML 的正文。我的解决了,因为我将转储 JSON 保存在文本文件中,所以在从文件中读取时,我的代码在前面添加了一些额外的空格,所以我收到了 400 错误。

有关更多信息,请尝试记录响应内容,它必须向您显示错误消息。


推荐阅读