Search code examples
c#jsonapiasp.net-core-2.2

.net core 2.2 unable to handle encrypted string in body even though mediatype says application/json


I have a third party that is posting a request to my API... they are posting encrypted text as the body but also including application/json as the Content-Type. Is there any way I can get .Net Core 2.2 to handle this... I have tried...

    [HttpPost("/callback/thirdparty")]
    [AllowAnonymous]
    public IActionResult Thirdparty([FromBody]dynamic body)
    {

and

    [HttpPost("/callback/thirdparty")]
    [AllowAnonymous]
    public IActionResult Thirdparty([FromBody]string body)
    {

But neither work.

    Request Headers
    Content-Type: "application/json"
    Accept: "*/*"
    Cache-Control: "no-cache"
    Accept-Encoding: "gzip, deflate"
    Content-Length: 155
    Connection: "keep-alive"
    Request Body
    z42T\/+j28AyZRslEXIZx9Vt87D1HOdMZqukzIi7s4gbtZqtdJJjJtQMkZ+O9vG3VlfJZ+6Ro7vwE\n6nTSwqczc2SwPiAaRjgv7nauvR2E3d8tk8zbVD+Ck6TL12hJHFHVySAP7I482lQU+SmE31OmDw==

Solution

  •     [HttpPost("/callback/thirdparty")]
        [AllowAnonymous]
        public IActionResult Thirdparty()
        {
            try
            {
                using (StreamReader reader = new StreamReader(Request.Body))
                {
                    var body = reader.ReadToEnd();
    

    Now I have a string in body!