Running Collection with a GET to an API and a POST to a Node JS not passing dynamic data

(28.04.2021 adding images that did not upload to original post)

The GET is to a vendor API on a local network server and the POST to a Node.JS on the Mac I am running Postman on.

I am lost in trying to follow the SpaceX launch demo, which appeared to be an example I could learn what I want to do with Postman. Perhaps there are better examples for my task?

The GET runs without error, the response body is the JSON I expect. A Test script filters objects and validates what I am looking for to console.log

I want to pass the JSON to a POST. The Post runs without error but the payload data sent to the node is unexpected. It only receives empty or mislabeled data so the Node JS script does not write what I want to the filesystem. I copied the node script.js substituting what I thought would be my specifics, a dynamic variable somewhere that would be updated each time the collection is run and passed to the Node. But where this variable is set in the POST? I tried global, in the collection, and in the POST. Is there another similar how-to example? I will bore someone here with my scripts and postman screens of what I have that isn’t working :slightly_smiling_face:

Thanks in advance.

GET
https://myServer.net/api/v1/projects

requires a bearer token and includes this Test

// script to filter operators with open projects
//
const responseJson = pm.response.json();
for (var i = 0; i < responseJson.projects.length; i++) {
var mounted = responseJson.projects[i].mounted_by;
var projectName = responseJson.projects[i].name;
var lastUsed = responseJson.projects[i].last_used_at;
if(mounted != ‘’) {
console.log('Operator-Workstation ’ + mounted + ’ Has Open Project(s): '+ projectName);

the GET request returns JSON containing multiple objects each with multiple name:value pairs inside a single array in a single object named projects:

the first object only is here as the dataset is large.

!

the Test filters non-empty values of named mounted_by in objects in the array and displays data, an example:

Operator-Workstation SkokanMacBookAir Has Open Project(s): Libr_JDE_GBL_Tassimo_Hero_the_coffee

Operator-Workstation PostprodCapo-MacPro Has Open Project(s): Proj_JCGramont_FRA_C21134388_Fragrance_OLV

Operator-Workstation Sivridi-MacMini Has Open Project(s): Proj_JDE_Jacobs_GBL_HP1148401_Presentation_Inspirational_Video

Operator-Workstation Valentin-MacPro Has Open Project(s): Proj_JDE_Lor_MAR_HP117541_CoffeeArtist_TVC

Operator-Workstation Valentin-MacPro,Pecak-iMac Has Open Project(s): Proj_JDE_Lor_SGP_HP1147453_BlendedPleasure_Capsules_OLV

Operator-Workstation Svatuska-iMac Has Open Project(s): Proj_JDE_Senseo_DEU_HP1113071_Earth_15s_TVC

Operator-Workstation Juzkova-MacbookAir,Sri-MacPro Has Open Project(s): Proj_Moneta_CZE_C11131687_INVESTICNI_FONDY

I confirm everything I want is in the JSON response from the API

POST

the x-www-form-unencoded from the SpaceX example along with setting a payload variable

I try a Pre-request, but I do not know the pm.variable to use

POST_preRequest

I set a variable in the collection also

the collection runs without errors and is running in the environment

Here is the script.js running in the Node modified as best I understand, which has no complaints but outputs and empty file

note: I reversed the order of the JSON.parse, and JSON.stringify after seeing errors and posts that existing JSON formats can cause this.
var express = require(‘express’);
var fs = require(‘fs’);
var bodyParser = require(‘body-parser’);
var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json()); // Body parser use JSON data
app.post(‘/projects’, function(req, res) {
var outputFilename = ‘./SBReport.json’; // path of the file to output
fs.writeFileSync(outputFilename, JSON.parse(JSON.stringify(req.body.payload), null, 4)); // write to the file system
res.send('Saved to ’ + outputFilename);
});
var port = 3000;
app.listen(port);
console.log(‘Express started on port %d …’, port)

And the empty output file resulting from the POST and node script

emptyFile

Hey Fred! Has this been resolved? If not here are a few considerations:

  1. Under “Headers” add the “Value” of application/json for Key: “Content-Type”

  2. What do you get when you console.log the req.body or bodyParser? You can create a test app.get and console.log req.body to check.

  3. Can you please share your app.get syntax? Your app.post syntax should mirror that one, but instead of “req.query” have it be “req.body”

Take a look at this FreeCodeCamp walk through of the latest Node, Express, and bodyParser middleware logging function: Get Data from POST Requests - Basic Node and Express - Free Code Camp - YouTube

Looking forward to hearing back from you!

Thank you for the reply Gianni,
I am still looking to find a solution. To your points:

  1. I think I understood. In the POST request I unselected the Content-Type variable which had been set as application/x-www-form-urlencoded - I believe from the SpaceX Demo example.
    I created and selected a new Content-Type key with the value application/json.


However when I rerun the collection with this change in the POST the node throws errors.
% node script.js
Express started on port 3000 …
SyntaxError: Unexpected token p in JSON at position 0
at JSON.parse ()
at createStrictSyntaxError (/Users/_svc_cz_transfers/node_modules/body-parser/lib/types/json.js:158:10)

  1. The request body of the GET is empty as I believe is expected. And the GET response is the JSON I am want. The Test is working fine. Perhaps you comment is for the POST request body, I am uncertain.

3 And I am uncertain the question here, sorry.

So, I will be looking at the FreeCodeCamp walk through today.
Hopefully this will give me ideas of what I am not understanding.
Thank you!
.

1 Like