Passing one endpoint response to another endpoint

Hey all,

i am new to tyk and still understanding how tyk works and how to configure and start working on it.

I want to know if we can pass one endpoint’s request to another endpoint? if so how is that achieved?

Thank you

Hi Nitesh,

You should be able to do this with the use of Virtual Endpoints. The documentation at the link below should help with this but let me know if you have any further questions.

https://tyk.io/tyk-documentation/compose-apis/virtual-endpoints/

Kind regards,

Jess @ Tyk

Thank you for the response @jess . One more question, Can we pass the response from one endpoint as a request to another endpoint? Is that possible through Virtual Endpoints?

Hi Nitesh,

This should be possible using Virtual Endpoints. You can do this by essentially capturing the response from a request to the first endpoint and using the body of said response to write the body of a request to the second endpoint. Below is an example of the code you could write:

var requestOne = {
    "Method": "GET",
    "Body": "",
    "Headers": {},
    "Domain": "http://foo.com",
    "Resource": "/widgets/1234",
    "FormData": {}
};

var responseOne = TykMakeHttpRequest(JSON.stringify(requestOne))

var requestTwo = {
    "Method": "POST",
    "Body": responseOne.Body,
    "Headers": responseOne.Headers,
    "Domain": "http://foo.com",
    "Resource": "/widgets/follow-on/",
};

var responseTwo = TykMakeHttpRequest(JSON.stringify(requestTwo))

var usableResponse = JSON.parse(responseTwo);
var responseObject = {
    Body: "THIS IS THE VIRTUAL RESPONSE"
    Headers: responseTwo.Headers,
    Code: responseTwo.Code
}

return TykJsResponse(responseObject, session.meta_data)

Hope that helps.

Kind regards,

Jess @ Tyk

Thank you so much @jess that was a good example.

Hi Jess, in the example the domain is explicitly specified and hard-coded in the JS code. But what if I just want to call API endpoint on the same Tyk Gateway? Is there a way to not specify the domain?

You could call local host and the appropriate listen path?