0
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 System.Net;
using System.IO;

namespace FunctionRestApi
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static IActionResult Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
    {

            var httpWebRequest = (HttpWebRequest)WebRequest.Create("URL_with_client_id_authorization_token");
            httpWebRequest.ContentType = "application/json";

            httpWebRequest.Method = "GET";

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
               var result = streamReader.ReadToEnd();
               log.LogInformation(result);
            }

            return new OkObjectResult(value: httpWebRequest);
      }
   }
 }

I am new to azure function. This code works when I am just using 'GET' method. But if I want to use 'POST' method with request body data i.e. date range (start_date and end_date) and some other sub_user_id, then how can I do that?

2 Answers 2

0

first check with POSTMAN how you can connect to external API with what authentication configuration BASIC/ BEARER then you can write code using same configuration

Sign up to request clarification or add additional context in comments.

Comments

0

Here is a simple example of a POST request using HttpClient with some comments:

using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

namespace AzureFunctionsSandbox.Functions
{
    public static class Function1
    {
        private static readonly HttpClient _httpClient = new HttpClient();

        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest request,
            ILogger log)
        {
            // create request as an object to easily set values
            var myRequest = new MyRequest
            {
                StartDate = DateTime.Now,
                EndDate = DateTime.Now.AddDays(1),
                SubUserId = "ABC123"
            };

            // serialize to JSON string for POST request body
            var myRequestJsonBody = JsonConvert.SerializeObject(myRequest);

            // .PostAsync() requires a HttpContent object - StringContent is a sub class
            var requestContent = new StringContent(myRequestJsonBody, Encoding.UTF8, "application/json");

            // make the POST request
            var response = await _httpClient.PostAsync("URL_with_client_id_authorization_token", requestContent);

            // use response body for further work if needed...
            var responseBody = response.Content.ReadAsStringAsync();

            return new OkResult();
        }
    }

    public class MyRequest
    {
        [JsonProperty(PropertyName = "start_date")]
        public DateTime StartDate { get; set; }

        [JsonProperty(PropertyName = "end_date")]
        public DateTime EndDate { get; set; }

        [JsonProperty(PropertyName = "sub_user_id")]
        public string SubUserId { get; set; }
    }
}

https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=net-5.0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.