(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
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
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