Reputation: 27852
I'm new to Azure Functions. I'm trying to write an Http Trigger that will not only "fail" bad json (that doesn't match my schema, I want to provide feedback to the caller with the invalid messages about the json they submitted.
Ok, so first I brought up VS2017.
Then I coded it up. I can use PostMan to test it, it works fine during PostMan testing.
using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
////using MyExceptionLibrary;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema;
namespace MyNamespace.AzureFunctionsOne
{
public static class MyFirstHttpTrigger
{
[FunctionName("MyFirstHttpTriggerFunctionName")]
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
{
log.Info("C# HTTP trigger function MyFirstHttpTriggerFunctionName about to process a request.");
try
{
string jsonSchemaText = @"{
'description': 'A person',
'type': 'object',
'properties':
{
'name': {'type':'string'},
'hobbies': {
'type': 'array',
'items': {'type':'string'}
}
}
}";
JSchema schema = JSchema.Parse(jsonSchemaText);
var content = req.Content;
string jsonContent = content.ReadAsStringAsync().Result;
JObject jobj = JObject.Parse(jsonContent);
IList<string> messages;
bool valid = jobj.IsValid(schema, out messages);
if (!valid)
{
string errorMsg = string.Join(",", messages);
throw new ArgumentOutOfRangeException(string.Format("Bad Json. ({0})", errorMsg));
}
}
catch (Exception ex)
{
string errorMsg = ex.Message; //// ExceptionHelper.GenerateFullFlatMessage(ex);
log.Error(errorMsg);
return req.CreateResponse(HttpStatusCode.BadRequest, errorMsg);
}
log.Info("C# HTTP trigger function MyFirstHttpTriggerFunctionName processed a request.");
return req.CreateResponse(HttpStatusCode.OK);
}
}
}
I then "published" this azure function to the cloud.
My issue is now........how do I wire this into the Logic App Designer to be the trigger?
In the below, I'm able to add the generic-request-trigger.
In the below, I've also looked for ~my~ azure http trigger that I published, and no luck.
So I can't figure out how to get my custom Http-Trigger to be available in the Logic App designer so it can be the entry-point-trigger.
Am I missing some basic concept?
My end game is:
I want a third-party to POST some json to my azure-logic-app as an http-request. That should be the trigger. But I only want the trigger to keep running if they submit valid json. (This I know can be done via the generic request trigger). My caveat (and thus my custom http-trigger) is that I want the third-party to get schema violation messages so they know what they did wrong.
Upvotes: 2
Views: 214
Reputation: 3111
If I understand this correctly, you have a workflow you want 3rd party to invoke via HTTP request, and when the request body isn't well formatted, you want to return a friendly error.
So you coded up an Azure Function that expose itself as a request endpoint, and does the validation.
If that's the case, you will just need to have the Azure Function to invoke Logic App after success validation, and pass the original payload to Logic App. So you can create the Logic App with a request trigger, save and get the Url, and have Function call that Url.
Upvotes: 1