0

I have the following javascript code, to produce a post request.

   var postUrl = "http://localhost:3100/radiance";

   xhttp.open("POST", postUrl, true);
   xhttp.setRequestHeader('Content-type', 'application/json');

   let radFile = {"radfile":"long string"}
   let solarJson = {"key1":"value1","key2":"value2"}
   let latitude = 45
   let longitude = 24

   msgJson=JSON.stringify({'radFile':radFile,'speedSolar':solarJson,'latitude':latitude,'longitude':longitude})

   xhttp.send(msgJson);

The post request works absolutely fine with my express app code below.

var express        = require('express');

// require our dependencies

var express        = require('express');

var bodyParser     = require('body-parser');
var cors           = require('cors');
var app            = express();
var port           = process.env.PORT || 3100;

var corsOptions = {
  credentials: false
};

// use body parser

app.use(bodyParser.urlencoded({extended: true, limit: 1024 * 1024 * 10000, parameterLimit: 1000000*1000}));

app.use(bodyParser.json({type: 'application/json', extended: false, limit: 1024 * 1024 * 10000, parameterLimit: 1000000*1000}));

// route our app
var router = require('./app/routes');
app.use('/', router);

var server
// start the server
server = app.listen(port, function() {
  console.log('app started ' +port );
});

However I am struggling to replicate the post request above in post man for testing (purposes).

I believe that the following post request should do the trick but when my app recieves this post request the body of the request is always empty. Why? It seems that the body parser isnt doing it's job.

The body of the post request

The header of the post request

What I am doing wrong in my post request in postman, how can I replicate my javascript post request exactly?

Anton James
  • 385
  • 3
  • 6
  • 18
  • Is there a reason why you're sending the json in a file from Postman and not in the app? Do you have the same Headers set? – Danny Dainton Jan 29 '19 at 05:50
  • This was long-known issue in Postman [postman-app-support](https://github.com/postmanlabs/postman-app-support/issues/1104) & [StackOverflow](https://stackoverflow.com/questions/36540719/sending-multipart-form-data-content-with-postman-chrome-extension) – Şivā SankĂr Jan 29 '19 at 06:59
  • Use `x-www-form-urlencoded` on Postman, Where you setup `urlencoded` already on your code – Şivā SankĂr Jan 29 '19 at 07:01
  • @ŞivāSankĂr you are correct, since I have files to attach to the post request I gave up on postman and just built a simple node app for testing purposes by using Cors – Anton James Jan 29 '19 at 15:38

1 Answers1

0

You are doing two different types of request here. Your website code is sending a JSON string with the contents of the 'files' embedded into it:

let radFile = {"radfile":"long string"}
let solarJson = {"key1":"value1","key2":"value2"}
let latitude = 45
let longitude = 24
msgJson=JSON.stringify({'radFile':radFile,'speedSolar':solarJson,'latitude':latitude,'longitude':longitude})

gives you a body of

{'radFile': {"radfile": "long string"}, 'speedSolar': {"key1":"value1", "key2":"value2"}, 'latitude': 45, 'longitude': 24}

However, when you are using Postman as you are, you are submitting 2 different pieces, a file (or 2) and the form values - you can use the postman echo site to see what you are actually submitting, in case you have questions in the future: Echo of bad request

If you really want to replicate your original version, you can do this - a raw body of type json, and fill in the details: Echo of valid request

josh.trow
  • 4,861
  • 20
  • 31