Changing the status code from 200 to 201

I am unable to change the status code of a successful POST request from 200 Success to 201 Created. Despite changing the default response to 201 in the serverless.yml file (snippet below), I still get a 200 status code.

employeeCreate: handler: handler.employeeCreate events: - http: path: employee method: post responses: 400: statusCode: "400" default: statusCode: "201"

Do you get this when deployed or offline?

Because when using new Error('[404] Not found') I would still get 200 status code from serverless-offline but 404 from deployed version.

Thanks for the reply. Custom error status codes using new Error() are working fine, but 201 is a success rather than error.

The response status codes for 1.0-rc1 and rc2 appear to be hard coded. In particular

  • 200 appears to be the only success code (no 201 or 204)
  • No support for redirects
  • Most but not all 4xx codes

There is a plugin that might help you https://github.com/silvermine/serverless-plugin-multiple-responses

As of a couple of days ago the master branch how has support for the lambda-proxy integration. This allows you to set the HTTP response code and headers from your lambda so you can to return a 201.

Great explanation, thanks.

The non-proxy APIG integration doesn’t allow to set any other Status Code than 200 for successful return. That was one of the main downsides to it and why AWS implemented the new proxy integration.

Not entirely correct. You can configure the APIGW to return any status code you want on success. The limitation is that you can only set one default status code and that code is always returned on success. So if you want your success code to always be 201 (created) or 204 (no content) then you can do that. Note: Serverless 0.5 allowed this but 1.0 doesn’t.

That said… Lambda proxy integration is much more flexible and easier to use IMHO so I’m really glad it’s the default. It’s going to make it easier for web developers to move to APIGW/Lambda

Yup you’re totally correct @buggy! Thanks for that!

hey i need to change mystatus code, and its work well with lambda-proxy, but i need lambda intregration cuz i work with response and request…
how can i change it?

this is my .yml file

functions:
sendData:
handler: handler.send
events:
- http:
method: POST
path: send
integration: lambda
cors: true
response:
headers:
Content-Type: “‘text/html’”
template: $input.path(‘$’)
statusCodes:
201:
pattern: ‘’ # Default response method
409:
pattern: ‘.“statusCode”:409,.’ # JSON response
template: $input.path(“$.errorMessage”) # JSON return object
headers:
Content-Type: “‘application/json+hal’”

and this is my handler.js

‘use strict’;

module.exports.send = (event, context, callback) => {
let data = event.body.json_data;
let stats = 0;
let response;

if (stats == 0)
     {
      response = 
       {
          statusCode : 409,
          body: {
              Message : "Mobile Number is not define or not on our Database"
          }
        }
     }
 else
    {
      response = 
      {
          statusCode: 200,
          body: {
            "mobile" : data.mobile
          }
      }
    }

callback(null, response);
};

can anyone related? @buggy @flomotlik

@frengkys You can change your response codes by returning an error from your Lambda that triggers the correct response. See Status Codes in the documentation.

With Servlerss 1.0 you may also need to look at the multi responses plugin as support in this area isn’t as good in 1.0 as it was in 0.5.

Personally, I think you’re going to spend a lot of time mucking around trying to get this working and I’m not 100% sure you’ll get the desired result anyway. Can you tell me a little more about why you feel you can’t use lambda-proxy integration? It might be easier to solve that problem.

hai @buggy, cuz i work with POST request,… i wanna send json data from client to lambda, if i change lambda to lambda-proxy it will return error, than i found on doc that say if we want to work with requst and respond on api gateway we should use lambda intregration instead lambda proxy…

or there are another way to use lambda-proxy to send json request to serverless lambda ? cuz it’ll be pain if it will take a lot of time to mucking around with it

You should be able to call JSON.parse() on the body passed into your function and JSON.stringify() on the object you return in your body. That will handle converting between a string and Javascript object.

Having said that, I noticed the 1.0.3 release notes mentioned Custom Status code for non-proxy APIG integration (#2014). I haven’t had a chance to look at this properly but upgrading to 1.0.3 might give you the functionality you need.

ive been try it on v1.03 but the additional statusCode [409] not change, but [201] (default) change correctly

response:
headers:
Content-Type: “‘text/html’”
template: $input.path(‘$’)
statusCodes:
201:
pattern: ‘’ # Default response method
409:
pattern: ‘.“statusCode”:409,.’ # JSON response
template:
application/json: $input.path(“$.errorMessage”) # JSON return object
application/xml: $input.path(“$.body.errorMessage”) # XML return object
headers:
Content-Type: “‘application/json+hal’”

if i have json value with this format
{
“animals” : { “air” : “bird”, “water” : “fish”, “land” : "cat " }
}

im just call it like this right ? JSON.parse(JSON.stringify(event.body.animals)))
but why return

SyntaxError: Unexpected token u
at Object.parse (native)
this error always shown when i change lambda intregration to proxy…

see Cant change non proxy status code [apig]