aldred
aldred

Reputation: 853

Cannot Perform HTTP Request in Hyperledger Composer

I am trying to create a business network which demonstrates nondeterministic behaviour by requesting a random number from a URL (https://random-number-api.mybluemix.net/random).

However, when I invoke the transaction from, the following error appears:

TypeError: request.get is not a function

In the documentation (https://hyperledger.github.io/composer/latest/integrating/call-out), it is mentioned that request is global and can be used directly.

To test the URL,

curl -X GET https://random-number-api.mybluemix.net/random

I am using Composer 0.18.2.

The following is the code. The whole business network definition is found here: https://github.com/caveman7/nondeterministic

async function createStock(request) {
    const factory = getFactory();
    const namespace = 'org.nondeterministic';

    const stock = factory.newResource(namespace, 'Stock', request.stockId);
    const priceAsStr = await request.get({uri: 'https://random-number-api.mybluemix.net/random'});
    stock.price = parseInt(priceAsStr);
    //stock.price = getPrice();
    console.log('@debug - Stock Price Generated is: USD' + stock.price);
    const ar = await getAssetRegistry(namespace + '.Stock')
    await ar.add(stock);
}

const getPrice = () => {
  const price = Math.floor((Math.random() * 10000) + 1);
  return price;
}

Upvotes: 0

Views: 112

Answers (1)

Paul O'Mahony
Paul O'Mahony

Reputation: 6740

probably because you're providing request as an object to the TP. As shown above or https://github.com/caveman7/nondeterministic/blob/master/lib/script.js#L22

Errors as its 'not' got a method called get?. It should be async function createStock(createStock ) to match the parameter name in the transaction decorator (tracing the github script file from the above link) - and then you should be able to access request.get() as you would expect (since request.get came in in 0.18.1 IIRC).

Upvotes: 1

Related Questions