Azure Function Webhook reveice and API respondse

Ruan Smith 1 Reputation point
2021-11-25T18:22:21.953+00:00

Hi everyone,

I'm trying to use a Webhook alert to trigger and send a async message to a device using an Rest API. The serial number is in the webhook body and that should be used at the end of the URL, but in the coding this is not working:
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Net.Http;
using System.Net;
using RestSharp;
using System.Threading;
namespace RTCWebhooks
{
public static class RTCWebhooks
{
[FunctionName("RTCWebhooks")]
public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log)

    {

        log.LogInformation("C# HTTP trigger function processed a request.");
        string requestBody = String.Empty;
        using (StreamReader streamReader = new StreamReader(req.Body))
        {
            requestBody = await streamReader.ReadToEndAsync();
        }
        dynamic data = JsonConvert.DeserializeObject(requestBody);

        string name = req.Query["Asset.DeviceSerial"];
        name ??= data?.name;
        Console.WriteLine(name);


        var client = new RestClient("https://api.oemserver.com")
        {
            Timeout = -1
        };

        Console.WriteLine(client);

        var request = new RestRequest("/v1.0/asyncmessaging/send/" + name,Method.POST);
        //if (name == null)
        //{
        //    name = "223670";
        //}
        request.AddParameter("name", name, ParameterType.UrlSegment);
        request.AddHeader("Authorization", "Basic Secret");
        request.AddHeader("Content-Type", "application/json");
        request.AddHeader("Accept", "application/json");
        var body = "{\"MessageType\":256,\"CANAddress\":4294967295,\"Flags\":1,\"Data\":[1],\"ExpiryDateUTC\":\"2024-07-20T18:25:43.511Z\"}";
        //var body = new { MessageType = 256, CANAddress = 4294967295, Flags = 1, Data = "[0]", ExpiryDateUTC = "2025-07-20T18:25:43.511Z"};
        Console.WriteLine(body);
        request.AddJsonBody(body);
        //request.AddParameter("application/json", body, ParameterType.RequestBody
        IRestResponse response = client.Execute(request);
        string fullUrl = client.BuildUri(request).ToString();
        Console.WriteLine(fullUrl);
        Console.WriteLine("myresponse " + response);
        return new OkObjectResult(response.Content);
    }
}

}

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,261 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jaliya Udagedara 2,731 Reputation points MVP
    2021-11-25T22:26:16.063+00:00

    I am seeing an issue:

    request.AddParameter("name", name, ParameterType.UrlSegment);
    

    This is not required since you already append the name to the URL.

    If you are still getting the error, please provide the response.StatusCode and the value of response.Content?

    0 comments No comments