14

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?

Elliot Larson
  • 10,669
  • 5
  • 38
  • 57
  • 1
    I managed to change the content-type, but not remove the quotes, any news? – Korri Jul 17 '15 at 04:14
  • I haven't made any headway with this. I get the impression that Amazon is attempting to block you from outputting plain text content. It looks like they allow you to transform from JSON to XML, which lead me to believe you could transform to any content type. I can't find anything in their marketing material or documentation that specifically states that they don't want you to output plain text, but I can't figure out a way to do it. My guess is that they're trying to stop you from trying to use this as a full fledged hosting platform for web apps. – Elliot Larson Jul 18 '15 at 06:15
  • this answer allows you to send back basically anything: http://stackoverflow.com/a/33614870/2300810 – zhywu Jul 15 '16 at 15:28
  • I'm going to write just a comment so it can be picked up in search in the future. I wanted to have "API-GATEWAY response output with no quotes". the answer below worked. – Jane Yun Jul 12 '19 at 14:42

3 Answers3

35

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 Ikeda
  • 534
  • 2
  • 6
  • 11
  • 1
    This was useful when trying to return XML, as well. I set the Mapping Template to "application/xml" and in my code, I have `context.succeed('');` — sadly, the reminder to re-deploy was also helpful. – chandlervdw Dec 14 '15 at 20:51
  • This saves me hours. Should be the chosen answer. – KF Lin Apr 05 '16 at 09:51
  • This worked great, after I followed the last part to set Method Response to "text/plain" as well. – Eric Zinda Feb 26 '22 at 00:51
  • This works, but the `Content-Type` header of the response is still `application/json`. Anybody know how to get it to return a different `Content-Type`? – TranquilMarmot Jun 13 '23 at 18:28
  • Took me a little while to find this in the UI but this absolutely does the trick. One thing to note is that application/json is the default. text/html is another mapping to consider. – JimmyJames Jul 26 '23 at 21:56
1

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).

jvliwanag
  • 1,508
  • 1
  • 13
  • 29
  • So just on that, the method I'm using is JSONPath, so the input.path('$') refers to a JSON path element, ergo you need to return valid JSON: {"value" : "something"} – Anthony Ikeda Apr 06 '16 at 20:35
0

Old question, but just as a reference, this is what I did. You can set the type in the Lambda when you return to the API:

callback(null, {
  statusCode: 200,
  headers: {"content-type": "text/plain"},
  body: "any text..."
})

or

return {
  statusCode: 200,
  headers: {"content-type": "text/plain"},
  body: "any text..."
}
ofri cofri
  • 886
  • 10
  • 13