'SOAP Request with content type as application/json .net 5

I'm trying to create a method to send a http post request to a SOAP API and get a typed response.

this is what I've created so far..

private async Task<string> GetContasSoap(string request)
    {
        var content = new StringContent(request, Encoding.UTF8, "application/json");
        var contentRequest = new HttpRequestMessage(HttpMethod.Post, _http.BaseAddress);
        contentRequest.Headers.Add("SOAPAction", "");
        contentRequest.Content = content;
        var responseRequest = await _http.SendAsync(contentRequest, HttpCompletionOption.ResponseHeadersRead);
        var responseRequestString = await responseRequest.Content.ReadAsStringAsync();
        return responseRequestString;
    }

Here I'm invoking the method

private readonly HttpClient _http;
    private readonly ILogger<Contas> _logger;
    public Contas(HttpClient http, ILogger<Contas> logger)
    {
        _http = http;
        _logger = logger;
    }

    public async Task<string> GetContas(ContaRequest contentRequest)
    {
        var jsonRequest = JsonSerializer.Serialize<ContaRequest>(contentRequest);
        var response = await GetContasSoap(jsonRequest);
        return response;
    }

Unfurtnately this aint working.. I'm getting this as response from the API

retorn ok: <?xml version="1.0" encoding="UTF-8"?>

<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">env:Bodyenv:Faultenv:Codeenv:Valueenv:Sender</env:Value></env:Code>env:Reasonenv:TextBad Request</env:Text></env:Reason></env:Fault></env:Body></env:Envelope>

Can anyone guess what I'm doing wrong? Cheers guys



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source