# Changing the status code from 200 to 201

**URL:** https://forum.serverless.com/t/changing-the-status-code-from-200-to-201/477
**Category:** Serverless Framework
**Created:** [October 7, 2016, 3:20pm UTC](https://forum.serverless.com/t/changing-the-status-code-from-200-to-201/477 "2016-10-07T15:20:53Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![bryander](https://avatars.discourse-cdn.com/v4/letter/b/54ee81/32.png) [@bryander](https://forum.serverless.com/u/bryander)
#### Post date: [October 7, 2016, 3:20pm UTC](https://forum.serverless.com/t/changing-the-status-code-from-200-to-201/477/1 "2016-10-07T15:20:53Z")

</div>

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"`

---

<div class="post-metadata">

### Author: ![enesdo](https://avatars.discourse-cdn.com/v4/letter/e/ea666f/32.png) [@enesdo](https://forum.serverless.com/u/enesdo)
#### Post date: [October 9, 2016, 7:10am UTC](https://forum.serverless.com/t/changing-the-status-code-from-200-to-201/477/2 "2016-10-09T07:10:18Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![bryander](https://avatars.discourse-cdn.com/v4/letter/b/54ee81/32.png) [@bryander](https://forum.serverless.com/u/bryander)
#### Post date: [October 10, 2016, 6:03pm UTC](https://forum.serverless.com/t/changing-the-status-code-from-200-to-201/477/3 "2016-10-10T18:03:40Z")

</div>

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

---

<div class="post-metadata">

### Author: ![buggy](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/buggy/32/177_2.png) [@buggy](https://forum.serverless.com/u/buggy)
#### Post date: [October 10, 2016, 11:11pm UTC](https://forum.serverless.com/t/changing-the-status-code-from-200-to-201/477/4 "2016-10-10T23:11:21Z")

</div>

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](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](https://github.com/serverless/serverless/blob/master/docs/02-providers/aws/events/01-apigateway.md). This allows you to set the HTTP response code and headers from your lambda so you can to return a 201.

---

<div class="post-metadata">

### Author: ![bryander](https://avatars.discourse-cdn.com/v4/letter/b/54ee81/32.png) [@bryander](https://forum.serverless.com/u/bryander)
#### Post date: [October 11, 2016, 12:31am UTC](https://forum.serverless.com/t/changing-the-status-code-from-200-to-201/477/5 "2016-10-11T00:31:51Z")

</div>

Great explanation, thanks.

---

<div class="post-metadata">

### Author: ![flomotlik](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/flomotlik/32/3_2.png) [@flomotlik](https://forum.serverless.com/u/flomotlik)
#### Post date: [October 12, 2016, 2:47pm UTC](https://forum.serverless.com/t/changing-the-status-code-from-200-to-201/477/6 "2016-10-12T14:47:14Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![buggy](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/buggy/32/177_2.png) [@buggy](https://forum.serverless.com/u/buggy)
#### Post date: [October 12, 2016, 10:19pm UTC](https://forum.serverless.com/t/changing-the-status-code-from-200-to-201/477/7 "2016-10-12T22:19:40Z")

</div>

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

---

<div class="post-metadata">

### Author: ![flomotlik](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/flomotlik/32/3_2.png) [@flomotlik](https://forum.serverless.com/u/flomotlik)
#### Post date: [October 13, 2016, 3:16pm UTC](https://forum.serverless.com/t/changing-the-status-code-from-200-to-201/477/8 "2016-10-13T15:16:28Z")

</div>

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

---

<div class="post-metadata">

### Author: ![frengkys](https://avatars.discourse-cdn.com/v4/letter/f/9fc348/32.png) [@frengkys](https://forum.serverless.com/u/frengkys)
#### Post date: [October 21, 2016, 6:45am UTC](https://forum.serverless.com/t/changing-the-status-code-from-200-to-201/477/9 "2016-10-21T06:45:21Z")

</div>

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

---

<div class="post-metadata">

### Author: ![buggy](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/buggy/32/177_2.png) [@buggy](https://forum.serverless.com/u/buggy)
#### Post date: [October 21, 2016, 11:27am UTC](https://forum.serverless.com/t/changing-the-status-code-from-200-to-201/477/10 "2016-10-21T11:27:29Z")

</div>

@frengkys You can change your response codes by returning an error from your Lambda that triggers the correct response. See [Status Codes](https://github.com/serverless/serverless/blob/7cace77345f8707522f648de64b33651734a22a9/docs/02-providers/aws/events/01-apigateway.md#status-codes) in the documentation.

With Servlerss 1.0 you may also need to look at the [multi responses plugin](https://github.com/silvermine/serverless-plugin-multiple-responses) 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.

---

<div class="post-metadata">

### Author: ![frengkys](https://avatars.discourse-cdn.com/v4/letter/f/9fc348/32.png) [@frengkys](https://forum.serverless.com/u/frengkys)
#### Post date: [October 23, 2016, 7:47am UTC](https://forum.serverless.com/t/changing-the-status-code-from-200-to-201/477/11 "2016-10-23T07:47:13Z")

</div>

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

---

<div class="post-metadata">

### Author: ![buggy](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/buggy/32/177_2.png) [@buggy](https://forum.serverless.com/u/buggy)
#### Post date: [October 23, 2016, 10:46am UTC](https://forum.serverless.com/t/changing-the-status-code-from-200-to-201/477/12 "2016-10-23T10:46:46Z")

</div>

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](http://forum.serverless.com/t/release-1-0-3-of-serverless/610) release notes mentioned [Custom Status code for non-proxy APIG integration](https://github.com/serverless/serverless/releases) ([#2014](https://github.com/serverless/serverless/pull/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.

---

<div class="post-metadata">

### Author: ![frengkys](https://avatars.discourse-cdn.com/v4/letter/f/9fc348/32.png) [@frengkys](https://forum.serverless.com/u/frengkys)
#### Post date: [October 23, 2016, 6:43pm UTC](https://forum.serverless.com/t/changing-the-status-code-from-200-to-201/477/13 "2016-10-23T18:43:18Z")

</div>

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…

---

<div class="post-metadata">

### Author: ![buggy](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/buggy/32/177_2.png) [@buggy](https://forum.serverless.com/u/buggy)
#### Post date: [October 24, 2016, 11:49am UTC](https://forum.serverless.com/t/changing-the-status-code-from-200-to-201/477/14 "2016-10-24T11:49:06Z")

</div>

see [Cant change non proxy status code [apig]](http://forum.serverless.com/t/cant-change-non-proxy-status-code-apig/622)
