django-rest-framework接受JSON数据吗? [英] django-rest-framework accept JSON data?

查看:249
本文介绍了django-rest-framework接受JSON数据吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用。用户端点是: / api / v1 / users

I have created RESTFul APIs using django-rest-framework. The user endpoint is: /api/v1/users

我想创建一个新用户,所以我发送用户数据采用JSON格式:

I want to create a new user, so I send the user data in JSON format:

{
    "username": "Test1",
    "email": "test1@gmail.com",
    "first_name": "Test1",
    "last_name": "Test2",
    "password":"12121212"
}

我正在使用Google Chrome扩展程序Postman测试API。但是,发送请求后,用户数据无法保存。响应包含以下错误:

I am using Google Chrome extension Postman to test the 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:

推荐答案

您错过了在标头部分添加 Content-Type 标头的过程。只需将 Content-Type 标头设置为 application / json ,它就可以工作。

You have missed adding the Content-Type header in the headers section. Just set the Content-Type header to application/json and it should work.

请参见下图:

此外,您可能还需要在标题中包含CSRF令牌,以防出现错误 { detail: CSRF失败:CSRF令牌丢失或不正确。} 使用邮递员发出 POST 请求时。在这种情况下,添加一个 X-CSRFToken 标头,该标头的值也应为CSRF令牌值。

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. In that case, add an X-CSRFToken header also with value as the CSRF token value.

这篇关于django-rest-framework接受JSON数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆