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.

Expose empty object (representing no request body) as a constant

See original GitHub issue

I should be able to do something along the lines of …

import bodyParser, {emptyBody} from 'body-parser';

app.use(bodyParser.raw({
  type: 'application/json'
}));

app.use((req, res) => {
  if (req.body === emptyBody) {
    // ...
  }
});

Otherwise, unless I am missing something, there is currently no safe way to check if the request body is empty.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
dougwilsoncommented, Jan 4, 2018

Right, it is a breaking change, yes. 2.0 can come out at any point. The only reason it hasn’t come is that there was the strong desire to refactor the internals to support the generic parser case, which is very close to being done (finally!).

FWIW you can accurately detect the body with the additional import of the type-is module (which is already a dependency of this module) though require('type-is').hasBody export. That is the work-around I’m using within my own code base until 2.0.

I’m going to at least try and rebase the current 2.0 branch tonight and cut an alpha of it to npm to give it the proper push forward it needs at this point. This is just an aside, I’m not saying this would really help you in particular.

2reactions
dougwilsoncommented, Jan 4, 2018

Yea, we could do that. Right now the 2.0 branch is going to just make req.body = undefined when there is no body. Is that possibly a solution here?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Request body is an empty object - node.js - Stack Overflow
My app index file is this: const express = require("express"); const http = require("http"); const { Server } = require("socket.io"); const ...
Read more >
JSON:API — Latest Specification (v1.1)
“Resource objects” appear in a JSON:API document to represent resources. ... resource identifier object for non-empty to-one relationships.
Read more >
Web API implementation - Best practices for cloud applications
Learn about best practices for implementing a web API and publishing it to make it available to client applications.
Read more >
Writing REST Services with RESTEasy Reactive - Quarkus
Accessing the request body. Any method parameter with no annotation will receive the method body., after it has been mapped from its HTTP ......
Read more >
Swagger RESTful API Documentation Specification
A resource in Swagger is an entity that has a set of exposed operations. The entity can represent an actual object (pets, users....
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