简体   繁体   English

Python json POST请求状态为200,而预期状态为201

[英]Python json POST request status 200 while expecting status 201

I'm trying to do a POST request to a web server in an attempt to create a new resource. 我正在尝试向Web服务器发出POST请求,以尝试创建新资源。 I'm fairly new to both Python and the platform I'm trying to communicate with on the server, so I'm not sure if it's my python skills require a bit more practice or if I'm just not submitting a request correctly to the server. 我对要在服务器上进行通信的Python和平台都不太熟悉,所以我不确定这是否是我的python技能需要更多的练习,或者我是否只是未正确提交请求到服务器。 Here's my code: 这是我的代码:

import requests

apiKey = "[APIKEY]"
serverUrl = "http://192.168.0.5/api/v1.0/payments"

values = """
  {
    "clientId": 1,
    "method": 3,
    "createdDate": "2016-09-12T00:00:00+0000",
    "amount": 10,
    "note": "",
    "invoiceIds": []
  }
"""


headers = {
  'X-Auth-App-Key': apiKey,
  'Content-Type': 'application/json'
}

r = requests.post(serverUrl, data=values, headers=headers)
r

The server responds with a Response 200 (OK) message, but since I'm trying to create a new resource it should respond with a Response 201 (Created) message. 服务器以响应200(确定)消息进行响应,但是由于我尝试创建新资源,因此服务器应以响应201(已创建)消息进行响应。 Any ideas what I'm doing wrong? 有什么想法我做错了吗?

Try using https:// , 尝试使用https://

change serverUrl = "http://192.168.0.5/api/v1.0/payments" 更改serverUrl = "http://192.168.0.5/api/v1.0/payments"

to

serverUrl = "https://192.168.0.5/api/v1.0/payments"

This probably won't result in a different HTTP status code, but you can send JSON encoded dictionaries directly by using the json parameter to requests.post() . 这可能不会导致不同的HTTP状态代码,但是您可以使用json参数直接将JSON编码的字典发送给request.post requests.post() So values can be a dictionary, and requests will set the content type so you don't need to in headers : 因此, values可以是字典, requests将设置内容类型,因此您无需在headers

values =
  {
    "clientId": 1,
    "method": 3,
    "createdDate": "2016-09-12T00:00:00+0000",
    "amount": 123.45,
    "note": "",
    "invoiceIds": []
  }

headers = {'X-Auth-App-Key': apiKey}

r = requests.post(serverUrl, json=values, headers=headers)

It's just a little cleaner. 只是有点清洁。

Regarding the expected 201 response, is the resource actually created? 关于预期的201响应,是否实际创建了资源? Do you get a 201 response when using other tools such as curl ? 使用其他工具(例如curl时,您是否收到201响应?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:POST 请求状态码 200 不允许 json 转换 - Python: POST request status code 200 not allowing json conversion 测试Post API接收状态码200而不是201 - test for post API receiving status code 200 instead of 201 为什么在 python 中进行单元测试时,我期望状态代码为 200,但状态代码为 404? - Why am I expecting a status code of 200 but got a status code of 404 while unit testing in python? 为什么在我的帖子请求中得到返回码 200 而不是 201? (Python/请求/Json) - Why did I get return code 200 instead of 201 on my post request ? (Python /Requests/Json) 获取200状态代码而不是201或204 - Getting 200 status code instead of 201 or 204 Django Rest 框架 API POST 接收状态码 200 而不是 201 - Django Rest Framework API POST receiving status code 200 instead 201 Python请求循环,如果状态码不是200,则重试 - Python request loop, retry if status code not 200 Python POST 请求返回 404 状态代码,但 GET 请求返回 200 - Python POST Request returns 404 status code but GET request returns 200 Web 用 python 抓取,request.json() 显示 status_code 为 200 但无法提取 json 数据 - Web scraping with python, request.json() shows status_code of 200 but can not extract json data 期望 404 但得到 200 HTTP 状态码 - Expecting 404 but getting 200 HTTP status code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM