Asp.net core invalid JSON

I have a problem sending a post request to my asp.net Server.

My Json looks like this:

{
     "Name":"NothingZeile224","FimDocument":"PHhkZjp4ZW5zY2hlbWEuMDEwMj4=","FimCodelistPicks":[]
}

And when I send it via a HttpRequestMessage in the Body to my Server I get this response:
{
    "errors": {
        "": [
            "Unexpected character encountered while parsing value: {. Path '', line 1, position 1."
        ]
    },
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
    "title": "One or more validation errors occurred.",
    "status": 400,
    "traceId": "|1db3b81f-4ca934b691097e16."
}

If I delete the brace in my JSON so it looks like this:
"Name":"NothingZeile224","FimDocument":"PHhkZjp4ZW5zY2hlbWEuMDEwMj4=","FimCodelistPicks":[]

It works.

But I don’t get why? Does someone have an idea?

Thank you

Edit:

This is my Server Code:

[Produces("application/json")]
    [ApiController]
    public class MainController : ControllerBase
    {
        [HttpPost]
        [Route("api/ConvertToCirali")]
        public async Task<string> ConvertToCirali([FromBody] string PostSendFim)
        {
            try
            {
                PostModel pm = (PostModel)Newtonsoft.Json.JsonConvert.DeserializeObject(PostSendFim);

                XDocument fim = XDocument.Parse(System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(pm.FimDocument)));

                string jsonOutput = Newtonsoft.Json.JsonConvert.SerializeObject(pm.FimCodelists);

                string MyContent = null;

                using (HttpClient client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Add("Content-Type", "application/json");
                    using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "https://localhost:44398/api/FullCodelists"))
                    {
                        using (StringContent stringContent = new StringContent(Convert.ToBase64String(Encoding.UTF8.GetBytes(jsonOutput)), Encoding.UTF8, "application/json"))
                        {
                            request.Content = stringContent;
                            using (HttpResponseMessage response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead))
                            {
                                MyContent = await response.Content.ReadAsStringAsync();
                            }
                        }
                    }
                }

                List<MongoCodelist> ReturnedCodelists = new List<MongoCodelist>();
                ReturnedCodelists = (List<MongoCodelist>)Newtonsoft.Json.JsonConvert.DeserializeObject(MyContent);

                FormatterFimToCirali fftc = new FormatterFimToCirali(fim, ReturnedCodelists);

                Debug.WriteLine("Hier steht der Text: " + fftc.CiraliFile.ToString());
                return Convert.ToBase64String(Encoding.UTF8.GetBytes(fftc.CiraliFile.ToString()));
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }
    }

Sorry if it looks a bit weird, it is for a converter for files from a game

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You have declared your parameter as string, so you need to send it a JSON string, not a JSON object. This string is then deserialized in your code at

PostModel pm = (PostModel)Newtonsoft.Json.JsonConvert.DeserializeObject(PostSendFim);

You can skip a step and let the framework do the deserialization for you. Just change the parameter type to PostModel instead of string.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x