0

I am trying to implement Calling an HTTP or REST API from Transaction Processor Functions

So when I am requesting GET https APIs it is working but when I am trying http APIs it is not working and throwing error:

RequestError: TypeError: Failed to fetch

and in browser console varies with APIs:
net::ERR_SSL_PROTOCOL_ERROR
net::ERR_CONNECTION_CLOSED

I have tested http APIs in postman and they are working perfectly fine. This is the code:

transaction checkApi {
  o String api
} 

async function checkApi(tx) {

    const apiObj = await request.get({ uri: tx.api, json: true });
    console.log(apiObj);

});

Is it because of CORS? can someone please explain what is the issue?

Paul O'Mahony
  • 6,740
  • 1
  • 10
  • 15
Shubham Chadokar
  • 2,520
  • 1
  • 24
  • 45
  • For ERR_SSL_PROTOCOL_ERROR in the browser (eg Chrome) - on most occasions, this error is due to a problem with the server, in that it may be requiring a client authentication certificate - you can google suggestions to resolve - also another issue can be date & time settings in the browser. Otherwise suggest to tag `javascript` `node.js` for assistance. – Paul O'Mahony Oct 09 '18 at 14:14
  • So it means it is browser issue not composer. But when i am browsing the same api in a tab it is working. And if it is not composer issue then it should at least complete the transaction but it is throwing error while submitting it as type error – Shubham Chadokar Oct 09 '18 at 14:20
  • then its likely its a CORS issue, yes. You can check in the browser's network monitor under Developer tools to see the request info? See also answer 2 / 3 here -> https://stackoverflow.com/questions/42754388/uncaught-in-promise-typeerror-failed-to-fetch-and-cors-error (for uncaught promise) or here -> https://stackoverflow.com/questions/49343024/getting-typeerror-failed-to-fetch-when-the-request-hasnt-actually-failed for more info. – Paul O'Mahony Oct 09 '18 at 16:52
  • Is there any way that we can make it work? either disable the CORS or white listing few URLs? – Shubham Chadokar Oct 10 '18 at 11:59

1 Answers1

0

I think you are trying to execute this transaction through composer playground with your network deployed to web browser instead of local fabric server. if this is the case, you will get the error. because it could be CORS issue. I ran into same issue.

But same request.get() worked for me when Transaction was tested on local fabric server.

other thing you should do is try using request.get("yourURL"); Dont use json flag.