question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Empty response if result is null

See original GitHub issue

Why 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:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:20 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
lichangweicommented, Apr 27, 2018

Below is my case:

request(options, function(error, response, body){
    //...
    ctx.status = response.statusCode; //500
    ctx.body = body; //undefined
    console.log(ctx.status); //204, it confused me
    //...
});

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.

3reactions
damianobarbaticommented, Feb 24, 2021

@maapteh after 4 years I gave up and came up with a https://www.npmjs.com/package/koa-better-json

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found