使用 Postman 对 Django Web 服务的 Http 发布请求(需要登录信息) [英] Http post request to a Django webservice (need login info) using Postman

查看:12
本文介绍了使用 Postman 对 Django Web 服务的 Http 发布请求(需要登录信息)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向我之前实现的 web 服务发送一个 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.

推荐答案

Postman 从 chrome 接收 cookie,您可以使用 Postman 拦截插件检索它们.看这里

Postman receives cookies from chrome and you can retrieve them using the Postman interception plugin. See here

现在安装插件后:

  1. 创建一个新环境,以便存储环境变量
  2. 创建一个带有测试的方法,将 XSRF cookie 值存储在环境变量中,在测试选项卡中发布此代码

  1. Create a new environment so environment variables can be stored
  2. 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-token 的环境变量.

Now you will have an environment variable with xsrf-token in it.

  1. 保存您的方法

创建新帖子并在标题中添加 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.

你也可以参考这个发布.

以防万一:对于 ajax 请求,您可以参考 django 文档

Just in case : For ajax requests you can refer to the django docs

这篇关于使用 Postman 对 Django Web 服务的 Http 发布请求(需要登录信息)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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