Begineer in Azure Function

ankit kumar 101 Reputation points
2020-10-07T22:14:10.62+00:00

I am trying to call a function from Logic app and for that I have started creating a small function code in the portal itself with HTTP trigger, which looks something like the one below and it is basically going to convertbase64 a value in name tag. and the response what i get is

" 500 Internal Server error"

code:

#r "Newtonsoft.Json"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;

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

    string name = req.Query["name"];

    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
    dynamic data = JsonConvert.DeserializeObject(requestBody);
    name = name ?? data?.name;
    var hash = Convert.FromBase64String( name );
    var sb = new StringBuilder();
    foreach( var b in hash )
    {
        sb.Append( b.ToString("X2") );
    }
    sb.ToString()

    string responseMessage = sb.ToString()? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
                : $"Hello, {hash}. This HTTP triggered function executed successfully.";

            return new OkObjectResult(responseMessage);
}

my input passed is

{
"name": "INDiTF0Nq8hU6d4fHqu1dQ"
}

how can I get the sb.Tostring() value in the output of the function so that i can call this in Logic app. My this bunch of code if ran separately in c# will give me the correct output, i am just trying to append it in function app and get the response in there

var hash = Convert.FromBase64String("INDiTF0Nq8hU6d4fHqu1dQ==");
    var sb = new StringBuilder();
    foreach( var b in hash )
    {
        sb.Append( b.ToString("X2") );
    }
    sb.ToString()
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,544 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. JayaC-MSFT 5,526 Reputation points
    2020-10-08T05:44:59.647+00:00

    Hello @ankit kumar I tried your code. I am sharing my code :

    #r "Newtonsoft.Json"  
      
    using System.Net;  
    using Microsoft.AspNetCore.Mvc;  
    using Microsoft.Extensions.Primitives;  
    using Newtonsoft.Json;  
    using System.Text;  
      
    public static async Task<IActionResult> Run(HttpRequest req, ILogger log)  
    {  
        log.LogInformation("C# HTTP trigger function processed a request.");  
      
        string name = req.Query["name"];  
      
        string requestBody = await new StreamReader(req.Body).ReadToEndAsync();  
        dynamic data = JsonConvert.DeserializeObject(requestBody);  
        name = name ?? data?.name;  
      
        var hash = Convert.FromBase64String( name );  
         var sb = new StringBuilder();  
         foreach( var b in hash )  
         {  
             sb.Append( b.ToString("X2") );  
         }  
           
        string str = sb.ToString();  
      
        return str != null  
            ? (ActionResult)new OkObjectResult(str)  
            : new BadRequestObjectResult("Please pass a name on the query string or in the request body");  
    }  
    

    Couple of changes made. I have added using System.Text; for the StringBuilder and modified the return object.

    Output :

    30822-image.png

    Please confirm if this helps. If yes, then please "accept the answer" and "up-vote" so that it help others in the community.

    1 person found this answer helpful.
    0 comments No comments