Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

200 response status code becomes 204 when no response body is set #1257

Closed
cdubz opened this issue Jul 29, 2021 · 15 comments · Fixed by #1627
Closed

200 response status code becomes 204 when no response body is set #1257

cdubz opened this issue Jul 29, 2021 · 15 comments · Fixed by #1627
Assignees
Labels

Comments

@cdubz
Copy link
Contributor

cdubz commented Jul 29, 2021

Bug Report

Current Behavior

After upgrading to v8.0.0 and when setting response.statusCode to 200 and not setting a body, the actual response status code from serverless-offline is changed to 204. Adding a body to response works around the issue.

Sample Code

  • file: serverless.yml
service: 200-to-204-example

plugins:
  - serverless-offline

provider:
  name: aws
  runtime: nodejs12.x
  stage: dev

functions:
  hello:
    events:
      - http:
          method: get
          path: hello
    handler: handler.hello
  • file: handler.js
'use strict'

exports.hello = async function hello() {
  return {
    statusCode: 200,
  }
}

Expected behavior/code

Response should have an empty body and a 200 status code.

Environment

  • serverless version: v2.52.1
  • serverless-offline version: v8.0.0
  • node.js version: v12.21.0
  • OS: macOS 11.4

Additional context/Screenshots

This behavior differs from v7.1.0 (which maintains the 200 status code in the example) but does not seem to be specifically documented anywhere. It appears this is somehow related to the dependency updates in ed06c2d.

In my case I noticed this with a PUT request response where 204 surely would be a better status code when no body is present but the enforced change that now occurs is surprising and seems worth a mention in release notes if nothing else.

@reubenporterjisc
Copy link

Hi @cdubz, did you ever resolve this issue? We are experiencing the same thing...

@cdubz
Copy link
Contributor Author

cdubz commented Oct 8, 2021

@reubenporterjisc I did not -- in fact I forgot all about this. I'm dealing with few enough consumers that we were able to easily adapt and modify them to expect a 204 instead of 200. I do think this approach makes more sense and to be fair this happened a major version release so it seems reasonable enough.

@cdubz cdubz closed this as completed Oct 9, 2021
@aardvarkk
Copy link
Contributor

I just ran into this and was very surprised by it. I was returning a simple number as a count in the response for pagination purposes. I could return any size, but started getting mysterious 204s with the body stripped out when I tried to return the number 0. I'm guessing it's just due to a bad falsey check somewhere? To me, a 200 with a response of "0" is different than a 204 with no response body and one should not be converted to the other.

@cdubz
Copy link
Contributor Author

cdubz commented Oct 15, 2021

@aardvarkk I think that’s worth a separate issue. Does sound like a bug worth fixing.

@aardvarkk
Copy link
Contributor

@cdubz Well... the problem was definitely a bad falsey check. In my code, as it turns out, hah!

I had a little helper function before returning the response that was trying to be helpful and ended up casting too wide a net for determining whether responses were empty. I have confirmed that serverless-offline properly returns a simple number 0 as a response if called properly. My apologies for the confusion!

@ky-cheng
Copy link

@aardvarkk quick question if you want to return a 200 OK response with no body with serverless-offline how do you achieve that with this change? To test Xero webhook integration locally, the webhook expects a 200 OK response with an empty body to establish the webhook but now it is impossible as an empty body response automatically return 204 no content.

@aardvarkk
Copy link
Contributor

@ky-cheng In my case, it was my fault that I was returning 204s instead of 200s, so I can't speak to how to address the larger issue here. Could you just manually override the status code in the response?

@ky-cheng
Copy link

ky-cheng commented Feb 1, 2022

@ky-cheng In my case, it was my fault that I was returning 204s instead of 200s, so I can't speak to how to address the larger issue here. Could you just manually override the status code in the response?

Yeah I set response.statusCode to 200 with no body but serverless offline seems to auto change the status code to 204 when there is no body so I am not sure what I can do

@terozio
Copy link

terozio commented Feb 1, 2022

I have similar issues with local test for CORS / http OPTIONS call that is expected to return 200 but it's modified to be 204. Started after updating serverless to v3 and serverless-offline to latest 8.3.1. Previously empty 200 came through as expected.

@ky-cheng
Copy link

ky-cheng commented Feb 2, 2022

@cdubz would you mind opening this issue again since you have given the perfect reproduction example in your original post already.

Perhaps Serverless offline should only change status code to 204 when the body is empty and no status code supplied.

@cdubz
Copy link
Contributor Author

cdubz commented Feb 2, 2022

Sure. I don’t think we ever got a maintainer in this chain to confirm this as an acceptable behavior so re-opening seems reasonable.

@cdubz cdubz reopened this Feb 2, 2022
@ky-cheng
Copy link

ky-cheng commented Feb 2, 2022

Agree. I think the main point is that it behaves differently to how for example an AWS lambda function would. The same code would return 200 OK when deployed but return 204 No Content from serverless offline when statusCode 200 is set with no body return

@dnalborczyk
Copy link
Collaborator

dnalborczyk commented Sep 5, 2022

this was introduced by bumping hapi, the underlying http framework: #1235

@dnalborczyk dnalborczyk self-assigned this Sep 16, 2022
@colesiegel
Copy link

Any updates here?

I unfortunately spent quite a bit of time troubleshooting my controller code before I luckily found this issue.

@dnalborczyk
Copy link
Collaborator

fixed in v12.0.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants