简体   繁体   English

django-rest-framework 接受 JSON 数据?

[英]django-rest-framework accept JSON data?

I have created RESTFul APIs using .我使用创建了 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 中请求详细信息的样子:

Postman API 请求 django-rest-framework 错误

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:您需要执行两个步骤来完成此问题:

  1. Add Content-Type header with application/json value添加带有application/jsonContent-Type标头
  2. Add Authorization header with Token {YOUR_CUSTOM_TOKEN} value to pass CSRFToken添加带有Token {YOUR_CUSTOM_TOKEN}值的Authorization标头以传递 CSRFToken

Note : 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 数据,需要做几件事。

  1. Make sure application/json headers are sent: 'Content-Type: application/json'确保发送 application/json 标头: 'Content-Type: application/json'

  2. JSON Parser is selected in settings.pysettings.py中选择 JSON Parser

    REST_FRAMEWORK = {
        'DEFAULT_PARSER_CLASSES': [
            'rest_framework.parsers.JSONParser',
        ],    
    }

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

相关问题 如何使用django-rest-framework反序列化嵌套的相关JSON数据? - How to deserialize nested related JSON Data with django-rest-framework? 如何使用django-rest-framework从端点检索Json元数据 - How to retrieve Json meta-data from endpoint using django-rest-framework 在 Django-Rest-Framework POST 调用中,它更新了多对多字段,适用于 JSON 但不适用于表单数据 - In Django-Rest-Framework POST call which updates manytomany field works with JSON but not with form-data 使用django-rest-framework将模型对象序列化为JSON dict - Serialize model objects into a JSON dict using django-rest-framework django-rest-framework:使用相同的 ModelViewSet 渲染 HTML 和 JSON - django-rest-framework: Rendering both HTML and JSON with the same ModelViewSet 如何通过Django-Rest-Framework中的数据属性重构数据? - How to restructuring the data by the data's property in Django-Rest-Framework? 在 django-rest-framework 中解析多部分/表单数据 - Parsing multipart/form-data in django-rest-framework 在 django-rest-framework 中显示 HTML 个表而不是原始数据 - Show HTML tables in django-rest-framework instead raw data 如何制作一个采用POST数据的Django-Rest-Framework API? - How to make a Django-Rest-Framework API that takes POST data? 如何在 django-rest-framework 查询集响应中添加注释数据? - how to add annotate data in django-rest-framework queryset responses?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM