I have an endpoint in a .Net Core API running in a linux container
[HttpPatch] // inside controller called Customer
public async Task<IActionResult> PatchAsync([FromBody] TestObject entity)
{
return Ok(await _service.PatchAsync(entity));
}
The Poco
public class TestObject
{
[JsonProperty(PropertyName = "Code")]
public string Code { get; set; }
[JsonProperty(PropertyName = "Name")]
public string Name { get; set; }
}
from my Blazor app I have the following code to update the entity
public async Task<bool> UpdateEntity(TestObject entity)
{
var content = new StringContent(JsonConvert.SerializeObject(entity), Encoding.UTF8, "application/json");
using (var httpClient = new HttpClient())
{
using (var response = await httpClient.PatchAsync(@"http://someserverpath:5000/api/v1/Customer", content))
{
string apiResponse = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<bool>(apiResponse);
}
}
}
The issue I am having when I run the code locally on windows 10 it works. When I run this code inside a Linux docker container I get the following exception
Error: Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: M. Path '', line 1, position 1.
CodePudding user response:
Try something like this:
public async Task<bool> UpdateEntity(TestObject entity)
{
var content = new StringContent(JsonConvert.SerializeObject(entity), Encoding.UTF8, "application/json");
using var httpClient = new HttpClient();
using var response = await httpClient.PatchAsync(@"http://someserverpath:5000/api/v1/Customer", content);
string apiResponse = await response.Content.ReadAsStringAsync();
if (response.IsSuccessStatusCode) {
return JsonConvert.DeserializeObject<bool>(apiResponse);
} else {
throw new Exception("Something went wrong when.... See inner excpetion.", new Exception(apiResponse));
}
}
You most likely received a non successful response and need to return the exception thrown by the API. Handle it the way you normally would in your solution