ASP.Net Core API – “‘s’ is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0.”

If you create an ASP.Net Core API Controller, and you wish to create an endpoint that can accept any string, you would expect this to be correct:

using Microsoft.ApplicationInsights;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Serilog;

namespace My.Api.Controllers
  public class TestController : ControllerBase
    public string PostTest([FromBody] string text)
      return text;

But this endpoint will only accept strings in quotes, so posting string will not work, and you get the “‘s’ is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0.” error. But if you post “string”, the function works.

This is because the method accepts a JSON string, and not a raw string.

There is no direct way of allowing any string to be posted to your endpoint, as the endpoints always expects a type-strong value.

So what can you do?


If you know that the input is a JSON object, you can post an object instead:

public string PostTest([FromBody] object text)
  return text.ToString();

This will accept any JSON object (for example { “a”: “b” }), but not a clean string.


To accept any raw string, you need to indirectly read the request body:

public async Task<IActionResult> PostTest()
  using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
    string message = await reader.ReadToEndAsync();
    return message;

The downside of streaming the request body to a string is that you cannot test the endpoint using Swagger (or OpenApi as it is called today), as there is nowhere to write the request body. To test it you need a tool such as Postman or CURL.



About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in .NET Core, c# and tagged , , . Bookmark the permalink.

1 Response to ASP.Net Core API – “‘s’ is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0.”

  1. Pingback: Handling “415 Unsupported Media Type” in .NET Core API | Brian Pedersen's Sitecore and .NET Blog

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.