Adding Http-Header to Azure-Durable-Function-Status-Response - azure

I have implemented a durable function started by calling a http-call.
The Http-Endpoint looks like this:
public static async Task<HttpResponseMessage> HttpStart(
[HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequestMessage req,
[DurableClient] IDurableOrchestrationClient starter,
ILogger log)
Within the method I start the Orchestrator and at the end of the method I return the StatusResponse by calling starter.CreateCheckStatusResponse(req, instanceId)
I then call frequently the returned status-url to get the current status and in the end the result of the process.
Now I want to add a custom http-header to the status-url-response that contains some summary about the returned result.
I wasn't able to figure out how it could be possible to achieve it.

public static async Task<HttpResponseMessage> HttpStart(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestMessage req,
[DurableClient] IDurableOrchestrationClient starter,
ILogger log)
Here request is defined from the HttpRequestMessage class where you have the properties like Headers, Request URI, Content, to define the custom Headers passing to the Request and Custom (User-defined) Response that comes from the Http Request.
You can define the parameters to be passed in the Headers Object for adding the extra content in the Http Requests-Responses.
Refer to this SO 1 & 2 for the Sample Code Snippets regarding passing the custom Headers and can bind to the Http Request object req in the HttpStart Function Code.

Related

Azure Logic App - Return email contents as JSON

I'm a beginner with Azure and want to be able to have a Logic App to read the contents of an email and output it in JSON such as:
{
"from": "Azure#Microsoft.co.uk",
"subject": "Azure Exception - A task was canceled.",
"body": "An exception has occurred in an Azure function"
}
The logic app is very basic, I have a 'when an email is received trigger' leading then to my Function App in which i am passing the Email Body as the request body. I am currently getting the http body request stuff back from the logic app such as:
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta content="text/html; charset=us-ascii"><meta name="Generator" content="Microsoft Word 15 (filtered medium)"><style>
<!--
#font-face
{font-family:"Cambria Math"}
#font-face
{font-family:Calibri}
This is not what I want as the main bit of the email that I want is at the bottom as html.
In my function app I am doing the following, taking the req and
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 emailBodyContent = await new StreamReader(req.Body).ReadToEndAsync();
return new OkObjectResult(emailBodyContent);
}
I'm not really sure how this works but I'm returning the req.Body.
I am assuming that it might be something to do with the below? As we could do from: Email From, Subject: header
Any guidelines or answers is much appreciated.
My understanding that you would like to extract the mail body (plain text) rather than having the complete html text.
By default, in the logic app - the email body received is HTML content. To Extract the text content you can use the Content Conversion connector.
Output :
Alternative :
You could use the Body Preview - if the body is smaller - the body preview will have the plain text of the body - which will directly meet your requirement.
Update
To create JSON output you could make use of the Compose Action

Azure 404 not found when adding new function app to logic app

I have a simple Azure Logic app that is trigger by a new email and then passes the Body to my Function App to remove HTML.
I am getting a 404 not found error and I'm a bit stuck as to what is actually not being found.
I can click on the url and it tells me that my function app is up and running and I've selected the Function App from a list in the Logic App editor.
Are there any log files or something that can provide a bit more information as to what cannot be found?
I think the issue might be that my Function app hasn't published correctly. In VS I am getting a missing trigger attribute but I'm not sure what to add.
Code:
public static class Function1
{
[FunctionName("RemoveHTMLfunction")]
public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
log.LogInformation("HttpWebhook triggered");
string emailBodyContent = await new StreamReader(req.Body).ReadToEndAsync();
String updatedBody = WebUtility.HtmlDecode(RemoveHtmlMarkup(emailBodyContent));
return (ActionResult)new OkObjectResult(new { updatedBody });
}
If you just want to get the body without HTML of the email, you can use Body Preview:
According to the running results, the body obtained by Body has HTML, but Body Preview does not.

Azure function get path of http trigger

I have a http trigger function app which redirects myAPI.com to a version based on the header:
public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
var version = req.Headers["x-version"];
if(version.ToString() == "2"){
return new RedirectResult("https://v2.myAPI.com", false);
}
else{
return new RedirectResult("https://v1.myAPI.com", false);
}
}
This works. However if I have a path e.g myAPI.com/customPath it returns 404 for myAPI.com/customPath. I want to redirect to https://v1.myAPI.com/customPath. Thought this would be simple; however I am unable to get the original url from within the function app.
When I try req.Path.Value it gives me /api/HttpTrigger1. How can I get the path or full url of my original post address (myAPI.com/customPath)?
You could use Microsoft.AspNetCore.Http.Extensions.UriHelper.GetEncodedUrl(req) to get the full path. It returns the combined components of the request URL in a fully escaped form suitable for use in HTTP headers and other HTTP operations.

How to remove ContentType requirement from NServiceKit request

I am trying to make a RESTful web service using NServiceKit version 1.0.43. I want this to work without an outside service that is not including a ContentType in their header request. My web service is rejecting the calls with a "406 Unaccepted Content Type" although I have not set a default content type. How do I allow calls to this service without defining a ContentType?
I did something similar with a RequestFilterAttribute in ServiceStack 4.x. It may need some tweaking to work on NServiceKit's fork, but this gives you the general idea. If a Content-type header is not sent in, it defaults it to JSON:
public class ContentTypeFixFilter : RequestFilterAttribute
{
public override void Execute(IRequest req, IResponse res, object requestDto)
{
if (!req.Headers.AllKeys.Contains("content-type", StringComparer.CurrentCultureIgnoreCase))
{
req.ResponseContentType = MimeTypes.Json;
}
}
}
}

Servicestack IRequestLogger get the IHttpRequest

I am implementing the Logging of the request and response in ServiceStack. I wanted to get hold of IHttpRequest in my IRequestLogger.Log() method.
The IRequestContext does not have info like the IHttpRequest, Is there a way I can get that passed or accessed. I am trying to log the Request Headers, UserAgent, token etc.
The IRequestLogger.Log is injected with the current IRequest.
You can cast to IHttpRequest for HTTP Requests as well as get access to the underlying ASP.NET or HttpListener request by casting IRequest.OriginalRequest, e.g:
void Log(IRequest request, object requestDto, object response, TimeSpan elapsed)
{
var httpReq = request as IHttpRequest;
var aspReq = request.OriginalRequest as HttpRequestBase;
}

Resources