Krishnachandran E S
Krishnachandran E S

Reputation: 95

Encoding Query Parameters from API Gateway

I have an created a REST application in nodejs and also I have configured API gateway for the URLs. I have a URL with query parameters. When one request from postman with query params with special characters like +,\ , then I need to encode these from API gateway. Is it possible to do that? Someone please help with an answer... Thanks in advance

Upvotes: 3

Views: 2807

Answers (1)

Balu Vyamajala
Balu Vyamajala

Reputation: 10373

We need to override the query parameter in mapping template of IntegrationRequest, function we need in vtl is $util.urlEncode.

The moment we start overriding, we need to ensure to build entire request body, parameters, etc.

Template from here, i just added one additional function $util.urlEncode call to encode parameters. If query parameter value is Test4! it will be changed to Test4%21

#set($allParams = $input.params())
{
  "params" : {
    #foreach($type in $allParams.keySet())
    #set($params = $allParams.get($type))
    "$type" : {
      #foreach($paramName in $params.keySet())
      "$paramName" : "$util.escapeJavaScript($util.urlEncode($params.get($paramName)))"
      #if($foreach.hasNext),#end
      #end
    }
    #if($foreach.hasNext),#end
    #end
  }
}

enter image description here

Upvotes: 1

Related Questions