【问题标题】:Trying to do a POST request from node.js to Django尝试从 node.js 向 Django 发出 POST 请求
【发布时间】:2015-04-21 01:52:10
【问题描述】:

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

姜戈

 @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);
})

【问题讨论】:

    标签: javascript python django node.js


    【解决方案1】:

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

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

    【讨论】:

    • 抱歉,我不确定我是否理解您的回答。我想获取传递的表单值,我该怎么做?感谢您的回复。
    • 从字面上看,将print(HttpRequest.POST) 替换为print(request.POST)。 (显然,GET 也是如此)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-20
    • 1970-01-01
    • 2020-10-05
    • 2021-01-06
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    相关资源
    最近更新 更多