"error": "Error during virtual endpoint execution. Contact Administrator for more details."

Hi I’m new to the Tyk world apologies if my questions are very newbie.

I have an api with a virtual endpoint plugin using inline js. What is supposed to happen on my code is to call ABC’s custom endpoint to request for a new token. Do take note that I have tested the ABCEndpoint in postman and it works totally fine. Now I wanted to call the endpoint using the api virtual endpoint of tyk.io.

First I have tested using this inlinejs to see if it really returns the “hello world” and yes it works totally fine.

function myUniqueFunctionName(request, session, config) {
  var responseObject = { 
    Body: "Hello World", 
    Code: 200 
  }
  return TykJsResponse(responseObject, session.meta_data)
}

Now here is the code that I have implemented that returns this “Error during virtual endpoint execution…”. I have omitted the urlABCEndpoint, client_id and client_secret for security purposes.

function myVirtualEndpoint(request, session, config) {
  var urlABCEndpoint = "https://thedummyurl.com/v2/token";
  var payload = {
    grant_type: "client_credentials",
	client_id: "clientidvalue",
	client_secret: "clientsecretkey",
    account_id: "534004151"
  };

  var headers = {
    "Content-Type": "application/json"
  };

  var options = {
    method: "POST",
    body: JSON.stringify(payload),
    headers: headers
  };

  var responseObject = { 
    Body: "success", 
    Code: 200 
  }
  
  var responseErrorObject = { 
    Body: "error", 
    Code: 500 
  }
  return TykMakeHttpRequest(urlABCEndpoint, options)
    .then(function(response) {
      //return response;
	  return TykJsResponse(responseObject, session.meta_data)
    })
    .catch(function(error) {
      //return error;
	  return TykJsResponse(responseErrorObject, session.meta_data);
    });
}

Error on the postman.

In my core settings I have only selected “Authentication Token” as the Authentication mode and that’s just it.

Hopefully anyone can advise me if there’s anything wrong in my code or any extra configuration I need to do. Thank you very much in advance!

Hello @Yakapo07 and welcome to the community.

The error message usually depicts a syntax error in your JavaScript function. From examining your code, it appears the error may be in your .then and .catch.

The JS plugins and virtual endpoints are run in a Sandboxed JSVM powered by Otto engine that is compiled in the Gateway binary. You are limited to the barebones of a scripting language and the JS API functions we provide e.g. no support for ES6 and above.

We have a few examples that show how you can write and construct your syntax here.

Hope it helps

Hi Olu,

First of all thank you very much for your reply.
I have modified my code based on the samples. Now I’m getting 2 different types of error:

1st Error: 502 Bad Gateway
This happens when my responseObject is

var responseObject = {
      Body: JSON.stringify(response),
      Headers: {
        "Content-Type": "application/json"
      }
    }

OR

var responseObject = {
      Body: response,
      Headers: {
        "Content-Type": "application/json"
      }
    }

2nd Error: { “error”: “Error during virtual endpoint execution. Contact Administrator for more details.” }

var responseObject = {
      Body: JSON.parse(response),
      Headers: {
        "Content-Type": "application/json"
      }
    }

Below is my updated code.

function myVirtualHandlerGetHeaders (request, session, config) {
    //Make api call to upstream target
    newRequest = {
      "Method": "POST",
      "Body": {
        "grant_type": "client_credentials",
        "client_id": "l084wof4gdfallf0s",
        "client_secret": "if0zKVgBZZXBiBv8eeI",
        "account_id": "534004151"
        },
      "Headers": {"Content-Type":"application/json"},
      "Domain": "https://hiddenurl-yl3y.auth.marketingcloudapis.com",
      "Resource": "/v2/token",
      "FormData": {}
    };
    rawlog("--- before get to upstream ---")
  response = TykMakeHttpRequest(JSON.stringify(newRequest));
  rawlog("--- After get to upstream ---")
  log("response type: " + typeof response);
  log("response: " + response);

    var responseObject = {
      Body: JSON.parse(response),
      Headers: {
        "Content-Type": "application/json"
      }
    }
      
    rawlog("Virtual Test ended")
    return TykJsResponse(responseObject, session.meta_data)   
  }

Below is the expected response value.

{
    "access_token": "eyJhbGciOiJIUz",
    "token_type": "Bearer",
    "expires_in": 1079
}

May I know what is wrong in my code or did I miss something here?
Also where do I see the execution logs? I wanted to see if it passed through these lines…

rawlog("--- before get to upstream ---")
response = TykMakeHttpRequest(JSON.stringify(newRequest));
rawlog("--- After get to upstream ---")
log("response type: " + typeof response);
log("response: " + response);

Thank you so much in advance for the reply!

Regards,
Ian Jasper

You should be able to messages in the gateway logs. You might have to enable debug mode if I remember correctly.

Errors in the logs most of the times means a syntax error. So my guess is it doesn’t like

Body: JSON.parse(response),

For the first error you might want to add the Code field in the responseObject to verify. I know the value of Body needs to be a string so maybe using a static string to debug might help find the cause.

Hi Olu,

I created a simple TykMakeHttpRequest to simplify my question.

I followed the instructions written in Virtual Endpoint Demonstration

I wrote 2 sets of sample code but the TykMakeHttpRequest still returns undefined value.

1st Code

function TykIOMFCToken(request, session, config) {
 newRequest = {
 "Method": "GET",
 "Body": "",
 "Domain": "https://dummyjson.com",
 "Resource": "/http/200",
 "FormData": {}
 };
 response = TykMakeHttpRequest(JSON.stringify(newRequest));
 rawlog("response result:"+typeof response);
 var responseObject = {
 Body: JSON.stringify({"result v3":typeof response,"execution":"from dummyjson"}),
 Headers: {
 "x-test": "virtual-header",
 "x-test-2": "virtual-header-2"
 },
 Code: 200
 }
 return TykJsResponse(responseObject, session.meta_data) 
}

2nd Code

function TykIOMFCToken(request, session, config) {
 var urlEndpoint = https://dummyjson.com/http/200;
 rawlog("endpoint:"+urlEndpoint);
 response = TykMakeHttpRequest(urlEndpoint);
 rawlog("response result:"+typeof response);
 var responseObject = {
 Body: JSON.stringify({"result v1":typeof response,"execution":"from dummyjson"}),
 Headers: {
 "x-test": "virtual-header",
 "x-test-2": "virtual-header-2"
 },
 Code: 200
 }
 return TykJsResponse(responseObject, session.meta_data) 
}

Below is the result using both codes:

{“execution”:“from dummyjson.com”,“result v3”:“undefined”}

@Yakapo07 Could you share the version of your gateway?

I only observe the undefined response with the second code. I don’t observe it with the first.

curl localhost:8080/virtual-endpoint/1st_code
{"execution":"from dummyjson","result v3":"string"}

The first code works perfectly fine and is in line with Tyk Virtual Endpoint syntax. The request object must be used to form any http request you intend on processing.

Unlike the first, the second has that issue. Virtual Endpoints utilise the JavaScript API embedded in Tyk for it’s requests and therefore follows the same rules and limitations.

  • TykMakeHttpRequest(JSON.stringify(requestObject)): This method is used to make an HTTP request, requests are encoded as JSON for deserialisation in the min binary and translation to a system HTTP call.

Also, the URL endpoint isn’t in quotes and could cause an error during execution. I have attached my API definition, so maybe it would help Virtual Endpoint Execution · GitHub

Thanks Olu, I copied your TykIOMFCToken_1st_code endpoint blob and pasted it in my code then it worked. I am able to proceed now.

1 Like