I’ve this situation:
server side:
[ApiController]
[Route("[controller]")]
public class TestController : Controller
{
[HttpPost]
public IActionResult Action(byte[] value)
{
var jsonString = Encoding.UTF8.GetString(value);
var myObject = JsonSerializer.Deserialize<SampleObject>(jsonString);
return Ok(myObject);
}
}
Client:
public static class Program
{
public static async Task Main(string[] args)
{
await SendObjectAsync();
}
private static async Task SendObjectAsync()
{
var myObject = new SampleObject();
var jsonString = JsonSerializer.Serialize(myObject);
var byteArray = Encoding.UTF8.GetBytes(jsonString);
using var client = new HttpClient();
var content = new ByteArrayContent(byteArray);
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
var response = await client.PostAsync("http://localhost:5287/Main", content);
if (response.IsSuccessStatusCode)
{
var responseData = await response.Content.ReadAsStringAsync();
var returnedObject = JsonSerializer.Deserialize<SampleObject>(responseData);
Console.WriteLine($"Received: {returnedObject!.Text},{returnedObject.Number}");
}
}
}
In the response i get always this error:
StatusCode: 415, ReasonPhrase: 'Unsupported Media Type', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
Date: Fri, 16 Aug 2024 21:48:40 GMT
Server: Kestrel
Transfer-Encoding: chunked
Content-Type: application/problem+json; charset=utf-8
}, Trailing Headers:
{
}
I also tried to add the FromBody attribute in the Action params, but without success.
Can someone please explain? I want to send binary raw data.
Thanks.
2