Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Function, returning status code + JSON, without defining return in every part of logic

I have an Azure Function 2.x that reside on a static class that looks like this

[FunctionName("Register")]
public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post")]HttpRequest req, ILogger log)
{
    MyTypeClass defReturn = new MyTypeClass();
    HttpStatusCode defCode = HttpStatusCode.BadRequest;

    /*
    * Logics that might or might not changes
    * defReturn and defCode value
    */

    return StatusCode((int) defCode, JsonConvert.SerializeObject(defReturn))
}

How can i achieve the return StatusCode((int) defCode, JsonConvert.SerializeObject(defReturn)) part ? is there any such method or equivalent in Azure Functions 2.x ?

in Azure Functions 1.x i can do the equivalent with req.CreateResponse(defCode, defReturn) where req is HttpRequestMessage , but i'm trying to stick with 2.x template/standard

Additional explanation : The said Code should return HTTP 400 Bad Request with the defReturn as it's response body to the client. But when i change the defCode to HttpStatusCode.Accepted, it should return HTTP 202 Accepted with the same response body. How can i achieve this ?

Additional explanation#2 : (If i remember correctly) in ASP.NET Core 1.x i can exactly do like that, returning IActionResult by calling a static method StatusCode not StatusCodes (which is a static class that contains HTTP codes constants

Thank you

like image 874
Tommy Aria Pradana Avatar asked Feb 25 '19 02:02

Tommy Aria Pradana


People also ask

Where is function JSON in Azure function?

Folder structure. The code for all the functions in a specific function app is located in a root project folder that contains a host configuration file. The host. json file contains runtime-specific configurations and is in the root folder of the function app.

Can Azure function return a value?

Using the Azure Function return valueIn languages that have a return value, you can bind a function output binding to the return value: In a C# class library, apply the output binding attribute to the method return value. In Java, apply the output binding annotation to the function method.

Can Azure function return HTML?

Even though Azure Functions is mainly used to serve APIs and return JSON objects, it can also be used to help HTML content.

What are durable functions in Azure?

Durable Functions is an extension of Azure Functions that lets you write stateful functions in a serverless compute environment. The extension lets you define stateful workflows by writing orchestrator functions and stateful entities by writing entity functions using the Azure Functions programming model.


1 Answers

Quite late reply, but I was stumbling into the same problem today, so maybe this is helpful for other searchers

Option 1: Default Codes

This is stated in detail on the blog Here

Some codes like 200 and 400 are predefined and can be used by

return new OkObjectResult("Your message"); // 200
return new BadRequestObjectResult("Your error message"); // 400

These functions are not available for every known Status Codes but some of the most frequent.

Option 2: Manual setting Code

If you need specific codes, that are not provided by default, you can use the base classes and create them yourself.

To achieve the Teapot Response for example, you can just use

using Microsoft.AspNetCore.Http;

var result = new ObjectResult("Your message");
result.StatusCode = StatusCodes.Status418ImATeapot;
return result;

In this example, the Statuscode is used from the StatusCodes class, but you can use enter other codes as well (usually, just stick to these codes)

Also, the ObjectResult class offers additional formatting options, if needed.

like image 185
auX Avatar answered Oct 13 '22 00:10

auX