javascript - 尝试从 node.js 向 Django 发出 POST 请求

标签 javascript python django node.js

我正在尝试向我的 Django 脚本发送一个带有一些数据的 POST 请求。它只是供内部使用的东西,所以安全性不是问题,但它似乎不想打印任何东西。它打印“TEST”,所以我知道发布请求被正确接收,但根据 Django 文档,HttpRequest.POST 应该打印 POST 数据的字典。

Django

 @csrf_exempt
    def botdeposit(request):
        if request.method == 'GET':
            print(HttpRequest.GET)
            return redirect('/')
        elif request.method == 'POST':
            print('TEST')
            print(HttpRequest.POST)
            return redirect('/')

Node .js

var request = require('request');

// Set the headers
var headers = {
    'User-Agent':       'Super Agent/0.0.1',
    'Content-Type':     'application/x-www-form-urlencoded'
}

// Configure the request
var options = {
    url: 'http://127.0.0.1:8000/test',
    method: 'POST',
    headers: headers,
    form: {'key1': 'xxx', 'key2': 'yyy'}
}

// Start the request
request(options, function (error, response, body) {
    if (!error && response.statusCode == 200) {
        // Print out the response body
        console.log(body)
    }
    console.log(body);
})

最佳答案

request.POST 会。 HttpRequest 是一个类,您有它的实例。就像文档说 HttpRequest.method 是一个东西,但是你写的是 request.method

(是的,文档令人困惑,并且没有很好地显示类和实例值之间的区别。)

关于javascript - 尝试从 node.js 向 Django 发出 POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29761503/

相关文章:

javascript - 如何使用 nestfastify 编写来自 nestjs 异常过滤器的响应

python - 在 python selenium 中,如何找到元素的可见性?

python - 如何根据另一个列表列表的值对列表列表进行排序?

python - 如何将数据计算出的模型字段添加到其他模型中?

javascript - 400 或 500 级别的 Http 响应

javascript - d3.js:单击时更改 x 轴间隔

JavaScript 实时计算

Python:用另一个词替换出现的第一个词

python - 检查 Django 模型字段选项是否存在

javascript - Spring Security 阻止 js post 查询文件上传