using System; using System.Collections.Generic; using Amazon.Lambda.APIGatewayEvents; using Newtonsoft.Json; /* Follow PMF response standard */ public static class HttpService { public static APIGatewayProxyResponse CreateResponse(int statusCode, Object payload = null, Exception exception = null, bool? forcedResult = null, bool serializeToJson = true) { string body = ""; object responseBody = null; //Anything that is not a 200 returns false. but you can force a result if (forcedResult != null) { responseBody = new { result = forcedResult.Value, payload = payload }; } else { if (statusCode < 200 || statusCode >= 300) { responseBody = new { result = false, payload = payload }; } else { responseBody = new { result = true, payload = payload }; } } var check = exception != null ? body = exception.Message : body = serializeToJson ? (payload != null) ? JsonConvert.SerializeObject(responseBody) : string.Empty : responseBody.ToString(); ; var response = new APIGatewayProxyResponse { StatusCode = statusCode, Body = body, Headers = new Dictionary { { "Content-Type", "application/json" }, { "Access-Control-Allow-Origin", "*" } } }; return response; } }