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.

JSON "null" request body rejected as an empty request body

See original GitHub issue

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I have an action method which accepts a posted null value for the body:

		[HttpPost("acceptsNull")]
		public string Upload(WeatherForecast? forecast = null)
		{
			return JsonConvert.SerializeObject(forecast);
		}

However, when I post a JSON null to this endpoint I get a 400 response complaining that the body is empty.

Expected Behavior

Since the body is not empty and JSON-deserializes to the model parameter type just fine, I expect it to invoke the action with a null model.

If it can’t do that, I at least expect an error message that indicates nulls are not permitted rather than one claiming the body is empty (although if the body were actually empty instead of valid JSON I’d probably want this request to be rejected).

Steps To Reproduce

Call the above endpoint with:

using var client = new HttpClient();
using var response = client.PostAsJsonAsync(".../acceptsNull", default(WeatherForecast));
Console.WriteLine(response.StatusCode); // 400
Console.WriteLine(await response.Content.ReadAsStringAsync()); //  ...A non-empty request body is required....

Exceptions (if any)

No response

.NET Version

6.0.100

Anything else?

The workaround I found is to set [FromBody(EmptyBodyBehavior = EmptyBodyBehavior.Allow)], but I don’t really want to have to do this because I’d like to reject empty bodies and accept non-empty bodies which are JSON null.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
senbarcommented, Apr 1, 2022

What’s the error message I should put in? Is something like “Null is not permitted in request body” fine? Edit: I have opened pr draft to decide on what message should look like

1reaction
rjgottencommented, Mar 15, 2022

A difference here is that query strings do not have a native encoding for null

Technically they don’t have a native encoding at all. Everything after the ? save for the # and whatever meaning a server wants to attribute to it, is fair game. However, there are some particular schemes which grew into ad-hoc quote-unquote ‘standards’ such as the way the ASP.NET query string parsers and model binder work; the way jQuery’s serialization of objects to query strings works (which iirc is a derivative of how PHP parses query strings?)

One convention for representing null query string values, or rather: a value-less key - is simply ?key. Contrast with ?key= which represents the empty string.

The former is not supported entirely as such by ASP.NET, afaik. I think all the parsing methods the framework supports, either drop the key entirely or rather than a value-less key turn it into a key-less value - i.e. a null key with a value of "key".

(Well; at least it’s consistently inconsistent with the rest of the world 🤷‍♂️ )

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Null request body not getting caught by Spring ...
The problem arises when someone tries to post a request body of "null". This somehow gets through the @Valid annotation and is not...
Read more >
Use empty string, null or remove empty property in API ...
TLDR; Remove null properties. The first thing to bear in mind is that applications at their edges are not object-oriented (nor functional if ......
Read more >
Empty Post Body in the request.content field event...
I am trying to send some data in a Post Method of a Proxy without a target, when I try to access the...
Read more >
APIM- Empty original request body in send-request
I have an inbound policy where I need to take a list of strings from the request body and send a request to...
Read more >
Request JSON object is null - Genesys Cloud Integrations
I am trying to do insert with the third party rest api call. I set Contracts json like this JSON { "title": "Create...
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