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.

Deserialization fails when request is empty but a content-type header is present

See original GitHub issue

I recently updated from 4.0.0-beta1 to 4.2.0 and an endpoint where I use EmptyRequest began to fail with a System.Text.Json.JsonException (see details below).

I believe this error is due to this commit.

I’m working on creating a test to reproduce and then I’ll re-add the content-length check.

Exception:

System.Text.Json.JsonException: The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. Path: $ | LineNumber: 0 | BytePositionInLine: 0.
 ---> System.Text.Json.JsonReaderException: The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. LineNumber: 0 | BytePositionInLine: 0.
   at System.Text.Json.ThrowHelper.ThrowJsonReaderException(Utf8JsonReader& json, ExceptionResource resource, Byte nextByte, ReadOnlySpan`1 bytes)
   at System.Text.Json.Utf8JsonReader.Read()
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   --- End of inner exception stack trace ---
   at System.Text.Json.ThrowHelper.ReThrowWithPath(ReadStack& state, JsonReaderException ex)
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   at System.Text.Json.Serialization.JsonConverter`1.ReadCoreAsObject(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   at System.Text.Json.JsonSerializer.ReadCore[TValue](JsonConverter jsonConverter, Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   at System.Text.Json.JsonSerializer.ReadCore[TValue](JsonReaderState& readerState, Boolean isFinalBlock, ReadOnlySpan`1 buffer, JsonSerializerOptions options, ReadStack& state, JsonConverter converterBase)
   at System.Text.Json.JsonSerializer.ContinueDeserialize[TValue](ReadBufferState& bufferState, JsonReaderState& jsonReaderState, ReadStack& readStack, JsonConverter converter, JsonSerializerOptions options)
   at System.Text.Json.JsonSerializer.ReadAllAsync[TValue](Stream utf8Json, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken)
   at FastEndpoints.Endpoint`2.BindToModel(HttpContext ctx, List`1 failures, JsonSerializerContext serializerCtx, Boolean dontAutoBindForm, CancellationToken cancellation)
   at FastEndpoints.Endpoint`2.ExecAsync(HttpContext ctx, EndpointDefinition endpoint, CancellationToken cancellation)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

HEADERS
=======
Accept: */*
Connection: keep-alive
Host: localhost:7167
User-Agent: PostmanRuntime/7.29.0
Accept-Encoding: gzip, deflate, br
Cache-Control: no-cache
Content-Type: application/json
Postman-Token: d7a60dcd-a9a1-4884-bfcb-12a210f708b6

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
CoryCharltoncommented, Jun 3, 2022

Sounds good. I came up with option 4 and am submitting a PR now. It’s basically:

        if (isEmptyRequest)
        {
            req = new TRequest();
        } 
        else if (isPlainTextRequest)
        ...
0reactions
ksydexcommented, Jun 20, 2022

Just got into this error and was really sad tryna find the solution for like 1h 😦

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deserialization fails when Content-Type is null · Issue #789
I recently found a bug where a DELETE response object causes a null reference exception when the response meets the following: content-type: ...
Read more >
ServiceStack Json deserializing with wrong Content-Type
1 Answer. Deserializing into an empty object is the correct behavior for ServiceStack's serializer. It tends to be very forgiving. It creates ...
Read more >
Deserialize json request error - unexpected character
1 Answer 1 ... This error typically happens when the call you are making returns an error page that is HTML rather than...
Read more >
Logic Apps: HTTP action block not sending "Content-Type" ...
So in the request, I add a "Content-Type" header with a value of "application/JSON". NetSuite can then interpret that and send its data...
Read more >
Format response data in ASP.NET Core Web API
The response header containing content-type: application/json; charset=utf-8 . The request headers. For example, the Accept header.
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