designetwork(EN)

IT technical memo of networking

POST JSON Data by sync-request module (Node.js)

Testing of POST with sync-request which can execute HTTP GET by synchronous processing.

In this article, since it is asynchronous with the requst module of Node.js (JavaScript), I introduced the srnc-request module which can synchronize without callback function.

Return value from request module with synchronization. (Node.js)(Not Callback) - designetwork(EN)

Node.js source code

Source code is here. The points will be described later.

npm information

Command syntax for posting JSON officially is stated. This time I make information to POST with variables.

var request = require('sync-request');
var res = request('POST', 'https://example.com/create-user', {
  json: { username: 'ForbesLindesay' }
});
var user = JSON.parse(res.getBody('utf8'));

Items that can be used in the Options section are prescribed. In this time, instead of adding "Content-type: application/json" to the Header, I used json already defined.

  • json - sets body but to JSON representation of value and adds Content-type: application/json.

POST JSON to Redmine API and create a ticket

I have referred to here about POST of Redmine API. Thank you very much.

http://kyka.sakura.ne.jp/it/archives/254

Explain the source code.

As a point JSON at ticket registration has a different syntax from GET. It is necessary to specify various items with "\<index>_id".

function redminePost(){
  bodyJson = {
    "issue":{
      "project_id":13,
      // NG!! "project":{"id":13,"name":"WebSystem"},
      "tracker_id":1,
      "status_id":1,
      <snip>

The POST part is here. Since json contains Content-type, Header is not necessary.

  var response = request(
    'POST',
    'http://'+ticketUrl+'/issues.json?key='+ticketKey,{
    // no need //headers: headerJson,
    json: bodyJson
    });

Execution result

When the above source code is executed, the result is returned as follows.

Response code is 201, header / body contains various kinds of information and created ticket information.

Start

Response {
  statusCode: 201,
  headers: 
   { date: 'Sat, 19 Nov 2016 07:15:20 GMT',
     server: 'Apache/2.4.6 (CentOS) PHP/5.4.16 Phusion_Passenger/5.0.21',
     'cache-control': 'max-age=0, private, must-revalidate',
     'x-frame-options': 'SAMEORIGIN',
     'x-xss-protection': '1; mode=block',
     'x-content-type-options': 'nosniff',
     'x-request-id': '1c51e2d8-9f5e-44c2-b107-884a04a06d21',
     location: 'http://<Redmine URL>/issues/<TicketNumber>',
     etag: 'W/"39d9d5ced85e556ab368c6065c935908"',
     'content-length': '508',
     status: '201 Created',
     connection: 'close',
     'content-type': 'application/json; charset=utf-8' },
  body: <Buffer 7b 22 69 73 73 75 65 22 3a 7b 22 69 64 22 3a 31 31 36 2c 22 70 72 6f 6a 65 63 74 22 3a 7b 22 69 64 22 3a 31 33 2c 22 6e 61 6d 65 22 3a 22 57 65 62 e3 ... >,
  url: undefined }
 body
{ issue: 
   { id: 116,
     project: { id: 13, name: 'WebSystem' },
     tracker: { id: 3, name: 'Support' },
     status: { id: 1, name: 'New' },
     priority: { id: 2, name: 'Normal' },
     author: { id: 5, name: '<POSTed User>' },
     subject: 'Cannot POST JSON Data bt Node.js',
     description: '',
     start_date: '2016-11-19',
     done_ratio: 0,
     custom_fields: [ [Object], [Object] ],
     created_on: '2016-11-19T07:15:20Z',
     updated_on: '2016-11-19T07:15:20Z' } }

End

Conclusion - POST JSON Data by sync-request module (Node.js)

I was able to POST by sync process with sync-request. Without using callback functions and so on, it is possible to simply perform ordered processing with Node.js (JavaScript).


This Blog is English Version of my JP's.

Sorry if my English sentences are incorrect.

designetwork