【问题标题】:Post request returns 400 Bad Request in RobotFramework发布请求在 RobotFramework 中返回 400 Bad Request
【发布时间】:2022-02-15 21:13:38
【问题描述】:

我一直在使用 RobotFramework 发送 POST 请求 我的代码如下:

*** Settings ***
Library  RequestsLibrary
Library  JSONLibrary
Library  Collections


*** Variables ***
${API_Base_Endpoint}    http://thetestingworldapi.com/




*** Test Cases ***
Post_Req_Test
    Create Session          AddData            ${API_Base_Endpoint}
    ${body}=                create dictionary   first_name=Tester1  middle_name=NAN  last_name=Tester2  date_of_birth=01/01/1999
    LOG TO CONSOLE          ${body}
    ${header}=              create dictionary   Content-Type=application/json
    ${response}=            post on session     AddData    api/studentsDetails     data=${body}  headers=${header}

但由于某种原因,我收到“HTTPError: 400 Client Error: Bad Request for url: http://thetestingworldapi.com/api/studentsDetails"

我尝试通过 Postman 发送相同的请求,并且没有问题。

有什么想法可能会出错吗?

【问题讨论】:

  • body需要序列化成json,和本期stackoverflow.com/questions/70142412/…基本一致
  • @MatthewKing 谢谢!它起作用了,我只是想知道在我看到的每个教程中没有人执行序列化。再次感谢
  • 在@MatthewKing 的帮助下,我被引用到this solution 为我工作
  • 根据您的评论,我做了一些挖掘,似乎如果您将正文传递给 json,例如json=${body} 而不是 data=${body},你不需要我建议的序列化行
  • 是的,效果很好

标签: post robotframework http-status-code-400


【解决方案1】:

在@MatthewKing 的帮助下,我被引用到this 解决方案为我工作

【讨论】:

    猜你喜欢
    • 2012-01-12
    • 2020-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多