2

I want to pass parameters to HttpTrigger Type in Azure Function 2. I see an answer for Azure function 1 in below link.

How to pass parameters by POST to an Azure function?

Answer in the above link is await req.Content.ReadAsAsync<>(); I am looking the similar answer for Azure Function 2.

Here we are using HttpRequest class instead of HttpRequestMessage class.

Below is the code.

public async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");
            string data = .............;
        }
Md Farid Uddin Kiron
  • 16,817
  • 3
  • 17
  • 43
Praveen kumar
  • 225
  • 1
  • 6
  • 18

3 Answers3

7

Its Seems you are trying to send POST request to Azure Function V2. See the below code snippet.

Custom Request Class:

public class Users
    {

        public string Name { get; set; }
        public string Email { get; set; }

    }

Azure Function V2:

In this example I am taking two parameter using a custom class and returned that two class property as response.

public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            //Read Request Body
            var content = await new StreamReader(req.Body).ReadToEndAsync();

            //Extract Request Body and Parse To Class
            Users objUsers = JsonConvert.DeserializeObject<Users>(content);

            //As we have to return IAction Type So converting to IAction Class Using OkObjectResult We Even Can Use OkResult
            var result = new OkObjectResult(objUsers);
            return (IActionResult)result;
        }

Request Sample:

{
   "Name": "Kiron" ,
   "Email": "kiron@email.com"
}

PostMan Test:

enter image description here

Note: You are looking for await req.Content.ReadAsAsync<>(); this actually needed for sending a POST request from your function. And Read from that server responses. But remember that req.Content is not supported as reading post request in Azure Function V2 which have shown Function V1 example here

Another Example:

See the below code snippet:

public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            //Read Request Body
            var content = await new StreamReader(req.Body).ReadToEndAsync();

            //Extract Request Body and Parse To Class
            Users objUsers = JsonConvert.DeserializeObject<Users>(content);

            //Post Reuqest to another API 
            HttpClient client = new HttpClient();
            var json = JsonConvert.SerializeObject(objUsers);
            //Parsing json to post request content
            var stringContent = new StringContent(json, UnicodeEncoding.UTF8, "application/json");
            //Posting data to remote API
            HttpResponseMessage responseFromApi = await client.PostAsync("YourRequstURL", stringContent);

            //Variable for next use to bind remote API response
            var remoteApiResponse = "";
            if (responseFromApi.IsSuccessStatusCode)
            {
               remoteApiResponse = responseFromApi.Content.ReadAsStringAsync().Result; // According to your sample, When you read from server response
            }

            //As we have to return IAction Type So converting to IAction Class Using OkObjectResult We Even Can Use OkResult
            var result = new OkObjectResult(remoteApiResponse);
            return (IActionResult)result;
        }

Hope you understand, If you still have any query feel free to share. Thanks and happy coding!

Md Farid Uddin Kiron
  • 16,817
  • 3
  • 17
  • 43
  • any benefit to using custom DTO for simply `dynamic` such as `JObject` ? – Alex Gordon Jun 03 '19 at 17:29
  • @l--''''''---------'''''''''''' So far as I know, you could also use dynamic as it is type safe but if you already know what kind of data would come forward it better to used instead. When we use dynamic compiler need to search each data-type which is a bit costly. – Md Farid Uddin Kiron Jun 03 '19 at 19:34
5
public static async void Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)]
            HttpRequest req,
            ILogger log)
        {
            var content = await new StreamReader(req.Body).ReadToEndAsync();

            MyClass myClass = JsonConvert.DeserializeObject<MyClass>(content);

        }

You can have a custom class which can be sent in the form of JSON from the post request.

Ramprasad
  • 316
  • 3
  • 10
0

At first create Azure Function Project (or add to your solution) based on HTTP req. trigger. In the single class on created project you can see example. You passed JSON from the request body:

{"name":"John"}

and just convert it to variable in your code:

 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 = await new StreamReader(req.Body).ReadToEndAsync();
        dynamic data = JsonConvert.DeserializeObject(requestBody);
        string name = data.name;
        ...