Reputation: 3
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
Upvotes: 0
Views: 1472
Reputation: 151594
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
.
Upvotes: 1