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?
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.
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)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With