Http使用Postman向Django webservice发送请求(需要登录信息) [英] Http post request to a Django webservice (need login info) using Postman
问题描述
我想向之前实现的webservice发送http请求,需要用户登录。现在,我实现了一个表单页面,为我做这个,我需要更改每个不同的请求。
I want to send a http request to a webservice ,which I implemented earlier, that need the user to be login. Now, I implemented a form page that do this for me and I need to change it for every different request.
据我所知,Django需要csrftoken和sessionid允许请求。不幸的是,我不知道如何将这两个字段添加到Postman客户端并与我的Django服务进行交互。
As far as I know, Django need "csrftoken" and "sessionid" to allow requests. Unfortunately, I can not figure out how to add this two field to Postman client and interact with my Django services.
推荐答案
邮递员收到来自chrome的Cookie可以使用Postman拦截插件来检索。
看到这里
Postman receives cookies from chrome and you can retrieve them using the Postman interception plugin. See here
现在安装插件后:
- 创建一个新的环境,以便可以存储环境变量
-
创建一个测试方法,将XSRF cookie值存储在环境变量中,在测试选项卡中发布此代码
- Create a new environment so environment variables can be stored
Create a method with a test to store the XSRF cookie value in an environment variable, in the test tab post this code
var token = postman.getResponseCookie("XSRF");
postman.setEnvironmentVariable("xsrf-token", token .value);
现在你将有一个环境变量xsrf
Now you will have an environment variable with xsrf-token in it.
- 保存方法
创建新帖子并在标题中添加XSRF-Token-Header Key。
Create the new post and add XSRF-Token-Header Key in the header.
使用{{xsrf-token} }
Access the token value with {{xsrf-token}}
在运行新请求之前,请确保运行该方法,以便可以存储环境变量,然后在运行实际请求时将附加它
Now before running your new request make sure you run the method, so that it can store the environment variable, and then when you run the actual request it will append its value in the header.
您还可以参考这个 post 。
只是为了防止:对于 ajax 请求你可以参考django 文档
Just in case : For ajax requests you can refer to the django docs
这篇关于Http使用Postman向Django webservice发送请求(需要登录信息)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!