Steve
Steve

Reputation: 923

nodejs route.get to SEND a post request

The get to this route works fine.

But how do I take my route.get() and make it do a post? If the post is written in jquery, or express, or something else I don't care. Below I just used the jquery as an example.

router.get('/', function (req, res, next) {
    var url = 'blabla'
    $.post('anotherBlaBla'
        , { app: url }
    );
});

Upvotes: 0

Views: 745

Answers (2)

Steve
Steve

Reputation: 923

const request = require('request');

var url = 'blabla';

request.post(
    url
    , { json: { api: url } }
    , function (err, res, bdy) {
        if (!err && res.statusCode == 200)
            console.log(bdy)
    }
);

Upvotes: 1

cdaiga
cdaiga

Reputation: 4937

You can use http client axios

Performing a POST request

 var axios = require('axios');

 axios.post('/user', {
    firstName: 'Fred',
    lastName: 'Flintstone'
 })
 .then(function (response) {
    console.log(response);
 })
 .catch(function (error) {
    console.log(error);
 });

Upvotes: 1

Related Questions