0

I am trying to POST some data into a REST server generated via Hyperledger composer. I have a JSON file and I am trying to use NodeJS and the Request module to post. However, it seems like the request module can find my link.

This is where I am trying to POST my assets (this is on the REST server): REST server

I copied and pasted the url to this post section into my inputter.js code

Here is my current directory (I am running the inputter.js file): Directory

Here is my inputter.js code:

var request = require('request');
var requestData = require("./input.json");


var url = "http://localhost:3000/explorer/#!/models95certificateModel95certificate/models_certificateModel_certificate_create";

request({
    url: url,
    method: "POST",
    headers: {
        "content-type": "application/json",
        },
    json: requestData
    }, function (error, response, body) {
        if(error)
            throw error;
        console.log('statusCode:', response && response.statusCode);
        console.log('body:', body);
    });

and here is the JSON data I am trying to input:

[ 
    { 
        "$class": "models.certificateModel.certificate",
        "hash" : "36ba3cf8261e1d203eafcf102f6426a9f0ff38edc62df6d7359938b2f358d68b8ce568325ebcd1ddfb3f64ebe41b73fc84d8fecd9023f834b487738ae02c6370",
        "state": "VALID",
        "issuer": "45576f7f-2f07-43f7-9638-1dcaa37a9f94",
        "owner": "d804d827-ebe8-4c42-b4b3-2888bba676a8",
        "comments": "Issued Certificate"
    },
    { 
        "$class": "models.certificateModel.certificate",
        "hash": "fc8ab6b4f10c80fbf6219e91991a32708f2da3d6de6eb2a52ac78ca930293597fdc693533afee6caae501a4808ecbb3b5367383e5e2ab88b08aef15af61a6497",
        "issuer": "45576f7f-2f07-43f7-9638-1dcaa37a9f94",
        "owner": "d2f0cf4b-cb21-4bc8-b059-e83abaa13e60",
        "state": "VALID",
        "comments": "Issued Certificate"
    },
    { 
        "$class": "models.certificateModel.certificate",
        "hash": "c876cf2b110f3af71fba5bba6dd48d2a063f1806c307cd9ae3e50494d39847137eba464c81bb423342bac8b6084f3e900b85044bef3cec459fd10c5f6bea0ad5",
        "issuer": "45576f7f-2f07-43f7-9638-1dcaa37a9f94",
        "owner": "39dfa2eb-76b9-44e4-a9fa-a107fadd2d46",
        "state": "VALID",
        "comments": "Issued Certificate"
    } 
]

I did a manual entry of the data on the REST server and everything in the JSON file seems to be valid.

When I run my inputter.js application, though, I get a 404 error. Here is the body of the error:

body: { error: 
   { statusCode: 404,
     name: 'Error',
     message: 'Cannot POST /explorer/',
     status: 404,
     stack: 'Error: Cannot POST /explorer/\n    at raiseUrlNotFoundError (/Users/harshdeshpande/.nvm/versions/node/v8.11.3/lib/node_modules/composer-rest-server/node_modules/loopback/server/middleware/url-not-found.js:21:17)\n    at Layer.handle [as handle_request] (/Users/harshdeshpande/.nvm/versions/node/v8.11.3/lib/node_modules/composer-rest-server/node_modules/express/lib/router/layer.js:95:5)\n    at trim_prefix (/Users/harshdeshpande/.nvm/versions/node/v8.11.3/lib/node_modules/composer-rest-server/node_modules/express/lib/router/index.js:317:13)\n    at /Users/harshdeshpande/.nvm/versions/node/v8.11.3/lib/node_modules/composer-rest-server/node_modules/express/lib/router/index.js:284:7\n    at Function.process_params (/Users/harshdeshpande/.nvm/versions/node/v8.11.3/lib/node_modules/composer-rest-server/node_modules/express/lib/router/index.js:335:12)\n    at next (/Users/harshdeshpande/.nvm/versions/node/v8.11.3/lib/node_modules/composer-rest-server/node_modules/express/lib/router/index.js:275:10)\n    at /Users/harshdeshpande/.nvm/versions/node/v8.11.3/lib/node_modules/composer-rest-server/node_modules/express/lib/router/index.js:635:15\n    at next (/Users/harshdeshpande/.nvm/versions/node/v8.11.3/lib/node_modules/composer-rest-server/node_modules/express/lib/router/index.js:260:14)\n    at Function.handle (/Users/harshdeshpande/.nvm/versions/node/v8.11.3/lib/node_modules/composer-rest-server/node_modules/express/lib/router/index.js:174:3)\n    at router (/Users/harshdeshpande/.nvm/versions/node/v8.11.3/lib/node_modules/composer-rest-server/node_modules/express/lib/router/index.js:47:12)\n    at Layer.handle [as handle_request] (/Users/harshdeshpande/.nvm/versions/node/v8.11.3/lib/node_modules/composer-rest-server/node_modules/express/lib/router/layer.js:95:5)\n    at trim_prefix (/Users/harshdeshpande/.nvm/versions/node/v8.11.3/lib/node_modules/composer-rest-server/node_modules/express/lib/router/index.js:317:13)\n    at /Users/harshdeshpande/.nvm/versions/node/v8.11.3/lib/node_modules/composer-rest-server/node_modules/express/lib/router/index.js:284:7\n    at Function.process_params (/Users/harshdeshpande/.nvm/versions/node/v8.11.3/lib/node_modules/composer-rest-server/node_modules/express/lib/router/index.js:335:12)\n    at Immediate.next (/Users/harshdeshpande/.nvm/versions/node/v8.11.3/lib/node_modules/composer-rest-server/node_modules/express/lib/router/index.js:275:10)\n    at Immediate.<anonymous> (/Users/harshdeshpande/.nvm/versions/node/v8.11.3/lib/node_modules/composer-rest-server/node_modules/express/lib/router/index.js:635:15)' } }

I am relatively new to the request module and cannot figure out exactly what is happening here. Can the request module not read localhost addresses? What exactly am I missing here?

harsh
  • 85
  • 1
  • 3
  • 9
  • Your url is not correct. You don't submit requests to the /explorer path as this is the path for the test UI that is inbuilt into the rest server. If you use the explorer UI to test with it should tell you the url for the request and example CURL invocation. – david_k Jul 03 '18 at 09:48
  • Thank you! I found the right URL to find (it was listed inside the REST server). – harsh Jul 03 '18 at 17:37

1 Answers1

0

I think you are not exporting the input data.