Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

output plain text content from Amazon API gateway endpoint

Using Amazon's API Gateway I can create an endpoint that will call a lambda function that outputs plain text. However, when I make a request on the endpoint, the output comes back with the default content type of "application/json". This outputs the plain text response wrapped in quotes. I'd like to change the response header to "text/plain" so it just outputs the text unwrapped with quotes. Does anyone know how to do this?

like image 696
Elliot Larson Avatar asked Jul 11 '15 21:07

Elliot Larson


2 Answers

So I managed to get this working.

In the Integration Response, you need to add a new Mapping Template of type "text/plain"

In the box to enter the template type:

$input.path('$')

Or the path to the value you want to return and save the new Mapping Template (do not select a model!)

You will then need to re-deploy your API for the change to take effect.

One thing I had in place already, was the Method Response also set to "text/plain" using the Empty model. I'm not sure if this has an effect, but if the above doesn't work, just add that in.

like image 184
Anthony Ikeda Avatar answered Oct 24 '22 08:10

Anthony Ikeda


Anthony's way still left quotes on the output string. So to recap, on the integration response, create a new Mapping Template for type text/plain. It should have the value:

$input.path('$')

Now, if you run context.succeed("somestring"), the output would be "somestring", wrapped in quotes. This is because, it is stringified as a json term. As a nasty workaround, you may do something like:

var base = JSON.stringify;
JSON.stringify = function(given) {
  JSON.stringify = base;
  return given;
}
context.succeed("somestring");

As a side note, you can get more hints by reading through console.log(context.succeed).

like image 32
jvliwanag Avatar answered Oct 24 '22 08:10

jvliwanag