【问题标题】:NodeJS Oboe not sending request body to PHP serverNodeJS Oboe 未将请求正文发送到 PHP 服务器
【发布时间】:2017-10-06 00:51:25
【问题描述】:

我试图让 Oboe 随请求发送一些数据,但它似乎不起作用。这是我的简单测试脚本,我还包含了一个 request.js 示例,它可以正常工作:

// Doesn't work
var oboe = require('oboe');
oboe({
    method: 'POST',
    url: 'http://localhost:8440/oboe.php',
    body: JSON.stringify({
        foo: 'bar',
    }),
}).done(function(data) {
    console.log('oboe', data);
});

// Works
var request = require('request');
request({
    json: true,
    method: 'POST',
    url: 'http://localhost:8440/oboe.php',
    body: JSON.stringify({
        foo: 'bar',
    }),
}, function(error, response, body) {
    console.log('request', body);
});

这个输出:

$ node test.js
oboe { get: [], post: [], body: '' }
request { get: [], post: [], body: '"{\\"foo\\":\\"bar\\"}"' }

还有我用于测试的简单 PHP 文件:

<?php
die(json_encode([
    'get' => $_GET,
    'post' => $_POST,
    'body' => file_get_contents('php://input'),
]));

我确定我做错了一些简单的事情,但不知道是什么。

【问题讨论】:

    标签: php json node.js http oboe.js


    【解决方案1】:

    好吧,我想我明白了。似乎需要发送Content-Length 标头。

    var data = JSON.stringify({
        foo: 'bar',
    });
    oboe({
        method: 'POST',
        url: 'http://localhost:8440/oboe.php',
        body: data,
        headers: {
            'Content-Type': 'application/json',
            'Content-Length': data.length,
        },
    }).done(function(data) {
        console.log('oboe', data);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多