Empty response if result is null
See original GitHub issueWhy the following returns an empty response? null
is a valid json value.
const config = require('./package.json');
const http = require('http');
const koa = require('koa');
const cors = require('kcors');
const compress = require('koa-compress')
const noTrailingSlash = require('koa-no-trailing-slash');
const json = require('koa-json');
const body = require('koa-body');
const send = require('koa-send');
const router = require('koa-router')();
const app = new koa();
app.use(cors());
app.use(compress());
app.use(noTrailingSlash());
app.use(json({ pretty: true, spaces: 4 }));
app.use(body({ formLimit: '5mb', jsonLimit: '5mb', strict: false, multipart: true }));
app.use(async (ctx, next) => {
try {
await next();
}
catch(error) {
ctx.status = 400;
ctx.body = error.message || error;
}
});
router.all('/ciao', async ctx => {
ctx.body = null;
});
app.use(router.routes());
http.createServer(app.callback()).listen(8080);
Reponse is empty:
macbook:react-app damiano$ curl -v localhost:8080/ciao
* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> GET /ciao HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.51.0
> Accept: */*
>
< HTTP/1.1 204 No Content
< Vary: Origin, Accept-Encoding
< Date: Fri, 09 Jun 2017 14:17:57 GMT
< Connection: keep-alive
<
* Curl_http_done: called premature == 0
* Connection #0 to host localhost left intact
macbook:react-app damiano$
Issue Analytics
- State:
- Created 6 years ago
- Reactions:6
- Comments:20 (6 by maintainers)
Top Results From Across the Web
How to validate empty or null return on retrofit? - Stack Overflow
I tried if(response) , if(response!=null) and more but nothing worked. It always catches it like "not null". var post: List<UserData> ...
Read more >Handling a Null Response from an API
An API that returns a null response can result in some really strange errors. Learn how to handle a null in Fetch and...
Read more >Trying to assert empty/null/undefined value with no luck
I know that the field type is “undefined” when I ran some code I found to check it. //pm.test("To Check if Value is...
Read more >Avoid Check for Null Statement in Java - Baeldung
Learn several strategies for avoiding the all-too-familiar boilerplate conditional statements to check for null values in Java.
Read more >Ensure Non-Empty Result Assertion - TechDocs - Broadcom Inc.
When the If field is set to False, this assertion fails the test when it receives an empty response. If the result is...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Below is my case:
if ctx.body == null and ctx.status is 2XX or not set, we can set ctx.status to 204, otherwise it’s better do nothing.
@maapteh after 4 years I gave up and came up with a https://www.npmjs.com/package/koa-better-json