NodeJS Oboe not sending request body to PHP server

166 Views Asked by At

I'm trying to get Oboe to send some data with the request, but it doesn't seem to work. This is my simple test script, I have also included a request.js example which works fine:

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

This outputs:

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

And my simple PHP file for testing:

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

I'm sure I'm doing something simple wrong, but can't figure out what.

1

There are 1 best solutions below

0
Petah On

Ok, I think I figured it out. Seems its required to send the Content-Length header.

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