1

Im trying to send a http post request from Action1 to Action2 and instead of sending the payload as string Im sending it as stream for performance. Im getting the error.

Unsupported Media Type

[ApiController]
[Route("api/v1/test")]
[ExcludeFromCodeCoverage]
public class TestController : Controller
{
    private readonly IHttpCommunicator _httpCommunicator;
    private readonly IObjectSerializer _objectSerializer;

    public TestController(IHttpCommunicator httpCommunicator, IObjectSerializer objectSerializer)
    {
        _httpCommunicator = httpCommunicator;
        _objectSerializer = objectSerializer;
    }

    [HttpPost]
    [Route("Action1")]
    public async Task<IActionResult> Action1()
    {
        var payload = new DocumentEntity
        {
            Id = Guid.NewGuid().ToString(),
            Name = "My Document",
            Content = System.IO.File.ReadAllBytes(@"2705 v1 1.docx")
        };

        var stream = _objectSerializer.SerializeToStream(payload);

        var httpCommunicationRequest = new HttpCommunicationRequest
        {
            IsPlatformService = true,
            Url = "https://localhost:3333/api/v1/Action2",
            Method = HttpMethod.Post,
            Data = stream,
        };

        var httpCommunicationResponse = await _httpCommunicator.Send(httpCommunicationRequest);

        return new JsonResult(httpCommunicationResponse);
    }

    [HttpPost]
    [Route("Action2")]
    public IActionResult Action2(DocumentEntity documentEntity)
    {
        return new JsonResult(new { Success = true });
    }
}

1 Answer 1

1

This error is happening because the server does not support or does not understand the content type you are sending it. You have to make sure that the content type of the request matches what the server expects.

In your request you can set the ContentType like this (for example):

var httpCommunicationRequest = new HttpCommunicationRequest
    {
        IsPlatformService = true,
        Url = "https://localhost:3333/api/v1/test/Action2",
        Method = HttpMethod.Post,
        Data = stream,
        ContentType = "application/octet-stream" // Set correct content type
    };

You'll also have to update your Send method and set the Content Type header. Something like:

public async Task<HttpCommunicationResponse> Send(HttpCommunicationRequest request)
{
    using (var httpClient = new HttpClient())
    {
        var httpRequestMessage = new HttpRequestMessage(request.Method, request.Url)
        {
            Content = new StreamContent(request.Data)
        };

        if (!string.IsNullOrEmpty(request.ContentType))
        {
            httpRequestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue(request.ContentType);
        }

        var response = await httpClient.SendAsync(httpRequestMessage);
        var responseContent = await response.Content.ReadAsStringAsync();

        return new HttpCommunicationResponse
        {
            StatusCode = response.StatusCode,
            Content = responseContent
        };
    }
}

Last thing to do is update Action2 to read from the request as a stream.

var content = await reader.ReadToEndAsync();
        var documentEntity = _objectSerializer.DeserializeFromString<DocumentEntity>(content);
        
        return new JsonResult(new { Success = true });

This is how I fixed a similar issue in the past.

Another route to take would be to clearly state in your app-settings that you want to use "octet-stream" and you can utilize this application wide, but you'll have to research how to implement that in the controller.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.