# Cant change non proxy status code \[apig\]

**URL:** <https://forum.serverless.com/t/cant-change-non-proxy-status-code-apig/622>\
**Category:** Serverless Framework\
**Created:** [October 24, 2016, 2:51am UTC](https://forum.serverless.com/t/cant-change-non-proxy-status-code-apig/622 "2016-10-24T02:51:17Z")\
**Posts on this page:** 6\
**Page:** 1

<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 24, 2016, 2:51am UTC](https://forum.serverless.com/t/cant-change-non-proxy-status-code-apig/622/1 "2016-10-24T02:51:17Z")

</div>

hai i thing im kinda lost here, so i make new thread here…  
i use v1.0.3, i dont know this is bug or my silly mistake again but this is it…

i work with json, send json to my lambda function

if i use proxy-lambda it will always return error and i dont know how to get value from json… i already use JSON.parse and JSON.stringify and still cant fix my problem…  
always stack on

> Unexpected token u at Object.parse (native)  
> or json value is \> undefined

if i use lambda intregration i can take the value from json request but the costum status code not work(for me here is [409]…  
but but the default value for status code is totaly fine[201]…  
.yml file for intregration is like this

> integration: lambda  
> 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’”

> response =  
> {  
> statusCode : 409,  
> body: {  
> errorMessage : “Mobile Number is not define or not on our Database”  
> }  
> }

is there some one face problem like me?

---

<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:48am UTC](https://forum.serverless.com/t/cant-change-non-proxy-status-code-apig/622/2 "2016-10-24T11:48:29Z")

</div>

Your serverless.yml file should look something like this

```auto
functions:
  hello:
    handler: handler.hello
    events:
      - http:
          path: hello
          method: post
          integration: lambda
          response:
            headers:
              Content-Type: "'application/json'"
            template: $input.body
            statusCodes:
              201:
                pattern: ''
              409:
                pattern: '.*"statusCode":409.*'
                template: $input.path('$.errorMessage')

```

Your default response will now be 201 instead of 200. All you need to do to get a 201 response is return a success.

```auto
'use strict';

module.exports.hello = (event, context, callback) => {
  callback(null, event.body);
};

```

To have it return a 409 error you need you need to callback with an error object

```auto
'use strict';

module.exports.hello = (event, context, callback) => {
  let result = {
    "statusCode": 409,
    "error": "Something went wrong"
  }
  callback(JSON.stringify(result), null);
};

```

---

<div class="post-metadata">

**Author:** ![fazelmk](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/fazelmk/32/1434_2.png) [@fazelmk](https://forum.serverless.com/u/fazelmk)\
**Post date:** [December 21, 2017, 7:47pm UTC](https://forum.serverless.com/t/cant-change-non-proxy-status-code-apig/622/3 "2017-12-21T19:47:11Z")

</div>

Sorry to bring this back to life. But I’m new to serverless and AWS lambda with API Gateway. So I having a little trouble to return a 404 status code.

I have this in the serverless.yml for the get service

```
    -http
        response:
          headers:
            Content-Type: "'application/json'"
          template: $input.path('$')
          statusCodes:
            200:
              pattern: ''
            404:
              pattern: '.*"statusCode":404,.*'

```

and my lambda function returns

`callback( null, JSON.stringify({ statusCode: 404, body: { message: "Resource not found" } }) );`

I get a return with status 200. and the json in the body. In serverless-offline I get a status 200 and the json in the body. In lambda test I got  
http 500  
{  
“message”: “Internal server error”  
}

anyone can explain it to me why?

---

<div class="post-metadata">

**Author:** ![fazelmk](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/fazelmk/32/1434_2.png) [@fazelmk](https://forum.serverless.com/u/fazelmk)\
**Post date:** [December 22, 2017, 1:30pm UTC](https://forum.serverless.com/t/cant-change-non-proxy-status-code-apig/622/4 "2017-12-22T13:30:34Z")

</div>

I tried setting the callback in the error callback ( JSON.stringify… , null ); with no luck too

---

<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:** [December 28, 2017, 5:17am UTC](https://forum.serverless.com/t/cant-change-non-proxy-status-code-apig/622/5 "2017-12-28T05:17:09Z")

</div>

You JSON.stringify() the body content and return the rest as a Javascript object.

```javascript
callback(null, {
  statusCode: 404,
  body: JSON.stringify({ message: "Resource not found" })
});

```

---

<div class="post-metadata">

**Author:** ![fazelmk](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/fazelmk/32/1434_2.png) [@fazelmk](https://forum.serverless.com/u/fazelmk)\
**Post date:** [December 28, 2017, 8:52pm UTC](https://forum.serverless.com/t/cant-change-non-proxy-status-code-apig/622/6 "2017-12-28T20:52:04Z")

</div>

I got help in gitter.  
The problem was integration type set to lambda.  
So I removed from serverless.yml integration and response. Using the default integration lambda-proxy.  
And the body don’t need to be a string. At lest I’m passing a JSON object to it and it’s working great.

Thanks anyway.
