【问题标题】:How do I pass the username/password to authenticate a POST request to a Django rest-framework API from python code?如何通过用户名/密码从 python 代码验证 POST 请求到 Django rest-framework API?
【发布时间】:2020-04-19 13:03:14
【问题描述】:

这是我想在 python 代码中模拟的请求,在这里使用 httpie 进行

$ http --auth mucho:pass POST http://3333333.ngrok.io/sms/ msg="love conquers all" to="255123456"
HTTP/1.1 201 Created
Allow: GET, POST, HEAD, OPTIONS
Content-Length: 67
Content-Type: application/json
Date: Mon, 30 Dec 2019 20:31:33 GMT
Server: WSGIServer/0.2 CPython/3.7.5
Vary: Accept, Cookie
X-Content-Type-Options: nosniff
X-Frame-Options: DENY

{
    "id": 70,
    "msg": "love conquers all",
    "owner": "mucho",
    "to": "255123456"
}

我试过了

from requests import Request,Session
url="http://3333333.ngrok.io/sms/"
data = {
    "to": "255123456",
    "msg": "love conquers all",
    }
s=Session()
req=Request('POST',url,data=data)
preped=req.prepare()
preped.prepare_auth(("mucho","pass"),url)
resp=s.send(preped)

它有效,但我正在寻找一种可能使用标题的更简洁/更简单的方法。感谢您对我的任何帮助

【问题讨论】:

    标签: python-3.x django-rest-framework django-rest-auth


    【解决方案1】:

    如果您正在寻找一种方法来测试您的 api 端点,您可以使用 insomniapostman

    设置标题很容易。

    【讨论】:

      猜你喜欢
      • 2016-01-21
      • 2013-08-22
      • 2015-08-16
      • 2017-02-26
      • 1970-01-01
      • 2018-09-17
      • 1970-01-01
      • 2018-09-22
      • 2020-03-08
      相关资源
      最近更新 更多