joepie91 / node-bhttp

A sane HTTP client library for Node.js with Streams2 support.
62 stars 12 forks source link

text data for post/put/patch not setting content-type #7

Open bberry6 opened 8 years ago

bberry6 commented 8 years ago

Heres some example code...

var express = require('express');
var bodyParser = require('body-parser');
var bhttp = require('bhttp');

var app = express();
//app.use(bodyParser.json());
app.use(bodyParser.text());

app.post('/', function (req, res) {
  console.log("Server::headers: ",req.headers);
  console.log("Server::body: ",req.body);
  console.log("Server::params: ",req.params);
  res.sendStatus(200);
});

var server = app.listen(3000, function(){
   var opts = {
      url: "http://localhost:3000/",
      data: 'test'
   };
   bhttp.post(opts.url, opts.data)
   .then(function(res){
      console.log("Client::statusCode: ", res.statusCode);
      return bhttp.post(opts.url, opts.data, {headers: {"content-type":"text/plain"}});
   })
   .then(function(res){
      console.log("Client::statusCode: ", res.statusCode);
      process.exit();
   });
});

And the output...

Server::headers:  { 'user-agent': 'bhttp/1.2.1',
  'content-length': '4',
  host: 'localhost:3000',
  connection: 'close' }
Server::body:  {}
Client::statusCode:  200
Server::headers:  { 'content-type': 'text/plain',
  'user-agent': 'bhttp/1.2.1',
  'content-length': '4',
  host: 'localhost:3000',
  connection: 'close' }
Server::body:  test
Client::statusCode:  200

There isn't any content-type passed in the first post. Only when manually defining the content type can the text/plain type be set.

Thanks!

joepie91 commented 8 years ago

To do: