POST方法python - 检查状态代码

时间:2018-06-19 09:19:25

标签: python json

我需要帮助完成这项任务。 我有一个自动测试填充我的表单,并在按下保存按钮后添加一个元素。在我的例子中,它将产品添加到商店数据库。 我必须在按下SAVE按钮后检查是否收到代码200。 POST发送得很好。

我的代码:

    def methodPOST(auth, url, code, data):

         headers = {'charset': 'utf-8', 'Authorization': auth}
         response = requests.post(url, json=data, headers=headers)

      if not response.status_code == code:
         file = open("path", 'a')
         file.write(response.request.method)
         file.write("\n Code: " + str(response.status_code) + "\n 
           expected: " +str(code)+"\n")
         file.write(str(response.reason) + "\n")
         file.close()

我遇到了问题,因为我再次发送相同内容,然后检查代码。我得到代码409 [不是200],因为我试图再次添加相同的东西。当我按下SAVE按钮时,我无法检查POST及其STATUS CODE。 如何更改我的代码?

谢谢你的帮助!

2 个答案:

答案 0 :(得分:0)

409表示客户端的CONFLICT。

https://httpstatuses.com/409

当您尝试发送相同的信息时,您应该使用PUT方法而不是POST。

请参阅: - https://www.keycdn.com/support/put-vs-post/

答案 1 :(得分:0)

Can you do that? Is it not to be done? Check the code after clicking Submit. which runs the POST method. without re-sending POST to the same address?

相关问题