Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

129
Views
Waiting for newman response in HTTP Trigger azure

Overview: sending a request to node.js azure function. inside it, there is a Newman module to run a collection request. Newman returns event emitter which I should listen to it which means it runs asynchronously.

Goal: Because I am new in js and node js. How can I return the response from Newman and then returns it as the response body of the HTTP request I know that await will not wait for (on) listening because it runs asynchronously

MySolution That I write the response from Newman in a file. and make another request to azure function to get the response.? there is better than that?

const newman = require('newman'); // require newman in your project

// call newman.run to pass `options` object and wait for callback
async function run() {
    await newman.run({
        collection: require('./weather.postman_collection.json'),
        reporters: 'cli'
    }, function (err) {
        if (err) { throw err; }
        console.log('collection run complete!');
    }).on('request', function (error, args) {
        if (error) {
            console.error(error);
        }
        else {
            // Log the response body

            console.log(args.response.stream.toString());
            return args.response.stream.toString()
        }
    });

}

module.exports = {
    run
}

const pip = require('./pipline')

module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

    const name = (req.query.appName || (req.body && req.body.name));
    const responseMessage = name
        ? "Hello, " + name + ". This HTTP triggered function executed successfully."
        : "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.";
    const result = await pip.run(context)
    
    context.res = {
        // status: 200, /* Defaults to 200 */
        body: args.response.stream.toString()
    };
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Reading data from file is not a bad idea. If you are Okay with it. But here is the alternate solution to get all data when we call newman request.

This script is using the Newman .on('request') event, that will extract that information. Or you can use .on('beforeDone')If you wanted all the response bodies you may need to modify this slightly and maybe use the appendFileSync to capture all the responses from the requests in the collection.

I have modified your code please have a look

const newman = require('newman'); // require newman in your project

// call newman.run to pass `options` object and wait for callback

async function run() {
    await newman.run({
        collection: require('./weather.postman_collection.json'),
        reporters: 'cli'
        }, function (err) {
            if (err) { throw err; }
            console.log('collection run complete!');
        }).on('request', function (error, args) {
            if (error) {
            console.error(error);
            }
            else {
            // Log the response body
                    console.log(args.response.stream.toString());
            // return args.response.stream.toString()
                    Cosnt data = JSON.parse(args.response.stream.toString());
                    return data;
            }
        });
    }
    module.exports = {
        run
}

After the initial call only the headers have been read. So, to parse the body as JSON, first the body data has to be read from the incoming stream. And, since reading from the TCP stream is asynchronous, the .json() operation ends up asynchronous.

Note: the actual parsing of the JSON itself is not asynchronous. It's just the retrieving of the data from the incoming stream that i asynchronous.

Refer Link 1 & Link 2

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!