-1

I am trying to make a post request with c# through the Azure HttpTrigger function. Only that when executing it sends me thousands of errors. Can someone help me here?

This is my code:

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;

namespace Admin
{
    public static class addTimbres
    {
        public static readonly string url = "https://dev.facturacfdi.mx:8083/admindigital/WSDistribuidoresServices?wsdl";

        [FunctionName("addTimbres")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req)
        {
            string username = "distribuidor1";
            string pass = "r*6Og80*2";
            string id = req.Query["id"];
            string cantidad = req.Query["cantidad"];
            string precio = req.Query["precio"];
            string tipo = req.Query["tipoServicio"];

            var client = new RestClient("https://dev.facturacfdi.mx:8083/admindigital/WSDistribuidoresServices?wsdl");
            client.Timeout = -1;
            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "text/xml");
            request.AddHeader("Cookie", "AWSELB=9967379D183B464F07472AD23944C79D1D256893C16F7531AC9AEC11DFCF25BB286EED0AA8BE0779B4925DB1440897B768AE03876F852C644560C840DC57CFDF50A6CA647F; AWSELBCORS=9967379D183B464F07472AD23944C79D1D256893C16F7531AC9AEC11DFCF25BB286EED0AA8BE0779B4925DB1440897B768AE03876F852C644560C840DC57CFDF50A6CA647F");
            var body =
            @"<Envelope xmlns=""http://schemas.xmlsoap.org/soap/envelope/"">
            " + "\n" +
            @"    <Body> 
            " + "\n" +
            @"        <getUsuarios xmlns=""http://servicios.ws.distribuidores.forsedi.mx/"">
            " + "\n" +
            @"            <username xmlns="""">distribuidor1</username>
            " + "\n" +
            @"            <password xmlns="""">r*6Og80*2</password>
            " + "\n" +
            @"        </getUsuarios>
            " + "\n" +
            @"    </Body>
            " + "\n" +
            @"</Envelope>";
            request.AddParameter("text/xml", body, ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);
            Console.WriteLine(response.Content);

        }



    }
}

I understand that RestClient is used for this case, but I can't find out how it is implemented with Azure Functions.

Thanks.

  • Sorry, my magic crystal globe is failing so please enlighten me: what errors? What exactly is going wrong? – Peter Bons Feb 09 '22 at 19:11
  • Sure, when implementing the RestClien it throws me the following error: **The type or namespace name 'RestClient' was not found (are you missing a using directive or an assembly reference?)** – Martin Vargas Feb 09 '22 at 20:07
  • You have to add the `RestSharp` Nuget package to your project, and use the namespace like `using RestSharp` – Anand Sowmithiran Feb 10 '22 at 14:17

1 Answers1

0

You need to add a reference to the RestSharp NuGet package to your project. And also, at the top of your .cs file, need to refer the namespace.

 using RestSharp;

Also, you are sending xml as request body, check out the documentation, it is more appropriate to use the AddXmlBody method.

Anand Sowmithiran
  • 2,591
  • 2
  • 10
  • 22