TykMakeHttpRequest and URL params

Hello,

I am writing a middleware script and trying to make REST API call in the pre request handler. I am attempting to make a GET call with URL parameters using TykMakeHttpRequest. The documentation page (https://tyk.io/docs/tyk-api-gateway-v-2-0/javascript-plugins/tyk-javascript-api/) does not mention anything about URL params. Also I think that when I add the URL params to the end of “Resource” request field the params get URL encoded leading to 404 error. So what is the correct way to make a request with URL params using TykMakeHttpRequest?

Hi huokko

Can you share the code you’re using, with the URL params?

I am having the same issue. A simple request.

var newRequest = {
    "Method": "GET",
    "Headers": {"Accept", "application/json",
    "Domain": "http://httpbin.org",
    "Resource": "/get?q=foo+bar"
};

returns a 404 from httpbin.org. This is because it’s actually querying the URL http://httpbin.org/get%3Fq%3Dfoo%2Bbar

Full example:

function myVirtualTest(request, session, config) {
    // Set up a response object. Assume all will be successful.
    var response = {
        Body: ""
        Headers: { "Content-Type": "application/json", "Access-Control-Allow-Origin": "*" },
    };
    
    var newRequest = {
        "Method": "GET",
        "Headers": {"Accept": "application/json"},
        "Domain": "http://httpbin.org",
        "Resource": "/get?q=foo+bar"
    }
    var resp = TykMakeHttpRequest(JSON.stringify(newRequest));
    var useableResponse = JSON.parse(resp);
    log(useableResponse);
    response.Code = useableResponse.Code;
    response.Body = useableResponse.Body;

    return TykJsResponse(response, session.meta_data);
}

You will need to use the AddParams string map or the ExtendedParams (string map to string array) to add query params.

I couldn’t get either AddParams or ExtendedParams to work for me. It looks as though the TykMakeHttpRequest method maps the input to TykJSHttpRequest which has the structure

type TykJSHttpRequest struct {
        	Method   string
        	Body     string
        	Headers  map[string]string
        	Domain   string
        	Resource string
        	FormData map[string]string      
        }

so those two get dropped / unused.

My bad, thought you were modifying outbound - that does look like a bug…

You can put the data into the FormData array - if it’s a GET it should map to query strings…

Tried that. I agree it’s a bug.

@matiasb I’ve gotten the batch process to work…

function myVirtualTest(request, session, config) {
    // Set up a response object. Assume all will be successful.
    var response = {
        Body: ""
        Headers: { "Content-Type": "application/json", "Access-Control-Allow-Origin": "*" },
        Code: 200
    };
    
    var batch = {
        "requests": [
            {
                "method": "GET",
                "headers": {"Accept": "application/json"},
                "relative_url": "http://httpbin.org/get?foo=bar"
            }
        ]
    };

    var resps = TykBatchRequest(JSON.stringify(batch));
    var asJS = JSON.parse(resps)[0];  // heh. there's only one response.
    response.Body = asJS.body;
    return TykJsResponse(response, session.meta_data);
}