[英]django-rest-framework accept JSON data?
I have created RESTFul APIs using django-rest-framework .我使用django-rest-framework创建了 RESTFul API。 The user endpoint is: /api/v1/users
用户端点是: /api/v1/users
I want to create a new user, so I send the user data in JSON format:我想创建一个新用户,所以我以 JSON 格式发送用户数据:
{
"username": "Test1",
"email": "test1@gmail.com",
"first_name": "Test1",
"last_name": "Test2",
"password":"12121212"
}
I am using Google Chrome extension Postman to test the API.我正在使用 Google Chrome 扩展 Postman 来测试 API。 But, after sending the request, the user data is not saving.但是,在发送请求后,用户数据并没有保存。 The response contains this error:响应包含此错误:
{
"detail": "Unsupported media type \"text/plain;charset=UTF-8\" in request."
}
This is what the request details look like in Postman:这是 Postman 中请求详细信息的样子:
You have missed adding the Content-Type
header in the headers section.您错过了在标题部分添加Content-Type
标题。 Just set the Content-Type
header to application/json
and it should work.只需将Content-Type
标头设置为application/json
即可。
See the below image:见下图:
Also, you might also need to include a CSRF token in the header in case you get an error {"detail": "CSRF Failed: CSRF token missing or incorrect."}
while making a POST
request using Postman.此外,您可能还需要在标头中包含 CSRF 令牌,以防在使用 Postman 发出POST
请求时收到错误{"detail": "CSRF Failed: CSRF token missing or incorrect."}
。 In that case, add an X-CSRFToken
header also with value as the CSRF token value.在这种情况下,添加一个X-CSRFToken
标头,其值也作为 CSRF 令牌值。
I'm posting this answer in case someone is facing a problem like mine.如果有人遇到像我这样的问题,我会发布这个答案。
I'm working on a Front-End app using Angular 2 with an API made with Django Rest Framework and I used to send requests with the following headers:我正在使用Angular 2和使用Django Rest Framework制作的 API 开发前端应用程序,并且我曾经发送带有以下标头的请求:
'Content-Type': 'application/json'
And it was working fine until I tried it on Fire Fox and I couldn't load the needed data and I solved it with adding the following headers它工作正常,直到我在Fire Fox上尝试它并且我无法加载所需的数据,我通过添加以下标题解决了它
'Content-Type': 'application/json',
'Accept': 'application/json'
Here's an explanation, Content-Type
tells the server what is the content type of data is while Accept
tells it what content type the client side will accpet.这里有一个解释, Content-Type
告诉服务器数据的内容类型是什么,而Accept
告诉它客户端将接受什么内容类型。
Here's a nice clear answer about this issue:这是关于这个问题的一个很好的明确答案:
https://webmasters.stackexchange.com/questions/31212/difference-between-the-accept-and-content-type-http-headers https://webmasters.stackexchange.com/questions/31212/difference-between-the-accept-and-content-type-http-headers
You need to do two step to done this issue:您需要执行两个步骤来完成此问题:
Content-Type
header with application/json
value添加带有application/json
值Content-Type
标头Authorization
header with Token {YOUR_CUSTOM_TOKEN}
value to pass CSRFToken添加带有Token {YOUR_CUSTOM_TOKEN}
值的Authorization
标头以传递 CSRFTokenNote : if you want to authenticate with session, you don't need to do second step, but if you want use this API for mobile, you have to pass Authorization header to server注意:如果你想通过 session 进行身份验证,你不需要做第二步,但是如果你想在移动端使用这个 API,你必须将 Authorization 标头传递给服务器
I hope it helps我希望它有帮助
You need to define content type by setting the appropriate headers.您需要通过设置适当的标头来定义内容类型。 In case of Postman you need to set the following values under url field:如果是 Postman,您需要在 url 字段下设置以下值:
Header: "Content-Type"标题:“内容类型”
Value: application/json值:应用程序/json
I had to add the following to get this to work (I'm using node-fetch btw from the client side to do a POST):我必须添加以下内容才能使其正常工作(我正在使用来自客户端的 node-fetch btw 来执行 POST):
supportHeaderParams: true,
headers: { "Content-Type": "application/json; charset=UTF-8" },
Couple of things to do if you want to accept JSON Data using Django Rest Framework.如果您想使用 Django Rest Framework 接受 JSON 数据,需要做几件事。
Make sure application/json headers are sent: 'Content-Type: application/json'
确保发送 application/json 标头: 'Content-Type: application/json'
JSON Parser is selected in settings.py
在settings.py
中选择 JSON Parser
REST_FRAMEWORK = {
'DEFAULT_PARSER_CLASSES': [
'rest_framework.parsers.JSONParser',
],
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.