1

I want to send some post data to an api

10.11.12.13/new/request/create

this is an API to create a new request in portal. now I am making one application in NodeJs and want to create request from node js application. now I have to send in this format

{"user":"demo", "last_name":"test","contact":"989898989"}

so how can I send data on above url to create a new request.

I am a beginner in NodeJs and don't have much idea. any help will be appreciated.

Thanks in advance

chandra
  • 85
  • 2
  • 13
  • use request module to make request from nodejs app here https://www.npmjs.com/package/request – kingdynastyk Nov 29 '17 at 10:37
  • possible duplicate of https://stackoverflow.com/questions/6158933/how-to-make-an-http-post-request-in-node-js – Andrew Lohr Nov 29 '17 at 14:59
  • Possible duplicate of [How to make an HTTP POST request in node.js?](https://stackoverflow.com/questions/6158933/how-to-make-an-http-post-request-in-node-js) – Andrew Lohr Nov 29 '17 at 15:00

3 Answers3

1

I would recommend to use axios or any other request lib :

const axios = require('axios');

axios.post('10.11.12.13/new/request/create', {
  user: 'demo',
  last_name: 'test',
  contact: '989898989',
});
vesna
  • 335
  • 2
  • 12
1

here is an example using request module

    var headers = {
    'Content-Type': 'application/json'
}
var options = {
    url: "10.11.12.13/new/request/create" ,
    method: 'POST',
    headers: headers,
    json: true,
    body: {user:"demo", last_name:"test",contact:"989898989"}
}
request(options, function (error, response, body) {
    if (error) {
        //do something
    }
    console.log(body)//do something with response
})
0

You can use postman REST client for GET method using your URL and Body (which you want to post) and click on * Code * and select NodeJS and their you will find code generated for you to work with. Here is the link https://www.getpostman.com/docs/postman/sending_api_requests/generate_code_snippets

With my experience, it is good to start with Request package for node js. Here is the link for your reference: https://www.npmjs.com/package/request

Nagaraja Malla
  • 144
  • 1
  • 10