Azure Logic Apps / Azure Function : Truncate Body when i do a reader Of a HTTPRequest body

46 Views Asked by At

I have an azure logic apps and i use an azure function to calculate the SHA256 of a PDF file that i previously dowloaded from an another software (API REST).

My problem is that the result of the downloaded request is only the file in the body and when i pass it to my function there is less byte in it than the original.

At the end, the SHA256 is not valid and the next step of my logic apps (push the file to another software with REST API) is not running because the software calculate the SHA256 of the file and it is not the same of my azure function.

The PDF it self is not damaged during the process and we can open it.

Here is the log of my function :

azureFunctionLog

The problem for me is, why the length is less after the reading ?

And here is my code :

using System;
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using System.Security.Cryptography;
using System.Text;

public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");
    log.LogInformation("Length Req.body: " + req.Body.Length);
    log.LogInformation("Content Req.body: " + req.Body);

    var reader = new StreamReader(req.Body);
    reader.BaseStream.Seek(0, SeekOrigin.Begin); 
    var requestBody = reader.ReadToEnd();

    
    log.LogInformation("Length : " + requestBody.Length);

    using var hash = SHA256.Create();
    var byteArray = hash.ComputeHash(Encoding.UTF8.GetBytes(requestBody));
    string hex = Convert.ToHexString(byteArray);
    

    return new OkObjectResult(hex);
}

Thanks !

I try to change the reader function with this code :

string requestBody = await new StreamReader(req.Body).ReadToEndAsync();

Same problem.

1

There are 1 best solutions below

0
Gilloufcr On

Thanks @rene for your comment. In the meantime, I've found a solution.

And indeed, I did a bad comparison and the conversion makes a smaller byte that I have at the beginning.

So there is the code that functions correctly:

using var hash = SHA256.Create();
var byteArray = hash.ComputeHash(req.Body);
string hex = Convert.ToHexString(byteArray).ToLower();


return new OkObjectResult(hex);