Send code via post message is not working

113 Views Asked by At

I wanted to send code to some node application and I use the postman with post message and in the body I put the following:

module.exports = function() {
    var express = require('express'),
        app = express();
    app.set('port', process.env.PORT || 3000);
    return app;
}

in the header of the request I put

content-Type   application/text/enriched

in the node code I use the following

module.exports = function (app) {
    fs = require('fs');
    var bodyParser = require('body-parser');
    ...
app.post('/bb',function(req,res){
        var fileContent = req.body

and the file content is empty ,I was able to see that it works since it stops in debug

1

There are 1 best solutions below

2
On BEST ANSWER

If you want to add a custom content type then you need to keep two things in mind:

  1. Content type couldn't be "application/text/enriched", in other hand "application/text-enriched" is ok. Max two "words".
  2. You must provide a custom accept header on body parser configuration BUT body parser return you a buffer when you use custom header

See the example:

var express = require('express')
var app = express()
var bodyParser = require('body-parser')

app.use(bodyParser.raw({ type: 'application/text-enriched' }))

app.post('/demo', function(req, res) {
    console.log('POST DATA')
    console.log('STREAM', req.body)
    console.log('STREAM to STRING', req.body.toString())

    res.status(200).send('ok');
});

app.listen(3000);

You can test in your console with curl:

curl 'http://localhost:3000/demo' -d 'name=john&surname=doe' -H 'Content-Type: application/text-enriched'

I recommend you try to not use a custom content type header because things are easier. I hope that my explanation help you.