I need some help in writing an Azure function with CosmosDB trigger, which will capture some values from cosmosdb like below and create a POST call and trigger an API. Is it possible in Azure function?
Cosmosdb:
The POST API which needs to be passed through Azure function is like this.
Here is the example code to for passing POST in the Azure function
[FunctionName("TestPost")]
public static HttpResponseMessage POST([HttpTrigger(AuthorizationLevel.Function, "put", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
{
try
{
//create redis connection and database
var RedisConnection = RedisConnectionFactory.GetConnection();
var serializer = new NewtonsoftSerializer();
var cacheClient = new StackExchangeRedisCacheClient(RedisConnection, serializer);
//read json object from request body
var content = req.Content;
string JsonContent = content.ReadAsStringAsync().Result;
var expirytime = DateTime.Now.AddHours(Convert.ToInt16(ConfigurationSettings.AppSettings["ExpiresAt"]));
SessionModel ObjModel = JsonConvert.DeserializeObject<SessionModel>(JsonContent);
bool added = cacheClient.Add("RedisKey", ObjModel, expirytime); //store to cache
return req.CreateResponse(HttpStatusCode.OK, "RedisKey");
}
catch (Exception ex)
{
return req.CreateErrorResponse(HttpStatusCode.InternalServerError, "an error has occured");
}
}
Here are the few examples for Post in Azure functions
Azure function POST
Post Async Function.
Related
Hello All I am using Azure's vision analyze api to extract text from my documents,
here is the example code for your reference
//My main function fi.fullfile is the path of my uploaded document
AzureAnalyzeRequest(System.IO.File.ReadAllBytes(fi.FullName));
analyze function
static async void AzureAnalyzeRequest(byte[] byteData)
{
var client = new HttpClient();
var queryString = HttpUtility.ParseQueryString(string.Empty);
// Request headers
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "MyKey");
// Request parameters
queryString["language"] = "en";
queryString["pages"] = "1,2";
var uri = "https://url-ocr.cognitiveservices.azure.com/vision/v3.2/read/analyze?" + queryString;
HttpResponseMessage response;
using (var content = new ByteArrayContent(byteData))
{
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
response = await client.PostAsync(uri, content);
}
}
when the above function executed I am getting the error of 400 bad request
but when I tested my api on the below URL
Azure Vision api
it worked fine.
what I am doing wrong here?
According to this MSDOCS the api needs a Json object in the following form:
{
"url":""
}
I think you are passing a byte array, you need a Json object which will contain a URL of the image you want to process.
So here I have created a class called Poco which will host the URL variable.
public class Poco
{
public string url { get; set; }
}
Then I initialized the class and passed the URL then convert that object into a Json object.
Poco p = new Poco();
p.url = "<URL OF YOUR IMAGE>";
string json = JsonConvert.SerializeObject(p);
// Here we are converting the json string to stringcontent which we can pass to httpclient
StringContent data = new StringContent(json, Encoding.UTF8, "application/json");
Now all you have to do is call the Api:
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "<YOURKEY>");
var response = client.PostAsync(url, data);
Console.WriteLine(response.Result.StatusCode);
Console.WriteLine(response.Result);
If you want to use the byte array of image, then I think the content-type header should be application/octet-stream according to this MSDOC
I am able to hit my call back function from Zoho Sign webhook. But I am not able to figure out how can I receive the response that Zoho Sign sends to my callback URL. Their documentation: https://www.zoho.com/sign/api/#webhook-management
Below is my sample code that I am using to confirm that callback function is hit. It saves a sample data to DB to confirm it is being hit. But the response I am not being able to catch hold of. This is their help documentation that guides on the same, but that misses a working sample. https://help.zoho.com/portal/en/community/topic/webhooks-for-zoho-sign
[HttpPost]
public ActionResult Callback()
{
using (var context = new ZohoApiTestEntities())
{
var rowDetails = new tblWebhook();
rowDetails.PhoneNo = "7978704767";
//rowDetails.Notes1 = jsonObj.ToString();
context.tblWebhooks.Add(rowDetails);
context.SaveChanges();
}
return new HttpStatusCodeResult(HttpStatusCode.OK);
}
Finally, after a lot of hits and trials, this code worked for me. It's bad that after a lot of follow-up and calls with the Zoho team, I did not receive any help from them for many days.
[HttpPost]
public ActionResult Callback()
{
string rawBody = GetDocumentContents(Request);
dynamic eventObj = JsonConvert.DeserializeObject(rawBody);
using (var context = new ZohoApiTestEntities())
{
var rowDetails = new tblWebhook();
rowDetails.PhoneNo = "*********";
//eventObj comes in JSOn format with two keys, "requests" and "notifications" each containing a JSON object https://www.zoho.com/sign/api/#webhook-management
//you can get your required details like this
string recipientName = eventObj.notifications.performed_by_name.ToString();
rowDetails.Notes1 = recipientName;
context.tblWebhooks.Add(rowDetails);
context.SaveChanges();
}
return new HttpStatusCodeResult(HttpStatusCode.OK);
}
private string GetDocumentContents(HttpRequestBase Request)
{
string documentContents;
using (Stream receiveStream = Request.InputStream)
{
using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
{
documentContents = readStream.ReadToEnd();
}
}
return documentContents;
}
I am working with Azure Function to consume a SOAP Service and exposing the data in 5 REST endpoints.
What I did is, uses the
class ServiceFactory {
// properties
// String path, ILogger log, IConfig mappingConfig
//constructors
public IService CreateService() {
switch(path) {
case ServicePath.service1:
return new service1(log, mappingConfig);
case ServicePath.service2:
return new service2(log, mappingConfig);
case ServicePath.service3:
return new service3(log, mappingConfig);
case ServicePath.service4:
return new service4(log, mappingConfig);
}
}
}
and then, the caller method is the azure function
[FunctionName("ServiceFunction")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = "{path?}")]
HttpRequest req,
ILogger log, string? path)
{
// Validate Credential
var validatorResult = await ValidateCredential(_credential);
if (!validatorResult.IsValid)
{
var errors = validatorResult.Errors.Select(error => new
{
field = error.PropertyName,
error = error.ErrorMessage
});
return new BadRequestObjectResult(
JsonConvert.SerializeObject(
new
{
success = false,
message = errors
}
)
);
}
IService service = ServiceFactory(path, req, log, _mappingConfigurationProvider, _service, _credential).CreateService();
return await service.ServiceTask();
}
so the path is here to call different endpoints.
I am asked to implement each of the endpoint with different functions.
What will be the pros and cons here?
Pros:
Single responsibility per function, better maintainability, open closed principle
PS: Extract the common logic to a class and share it among the functions.
Cons:
I can't think any cons about this.
https://learn.microsoft.com/en-us/azure/azure-functions/functions-manually-run-non-http
I am trying to manual trigger my Azure Timer function App created in 2.0 and developed in .net core 2.0.
When I try to hit the url I get 403 error.
apikey I pass is picked from :
As the article you provided, you need to use _master key under Manage and Host key
I use the following class in my integration tests against service bus triggered Azure Functions.
class AzureFunctionCaller
{
private readonly HttpClient _httpClient;
private readonly string _functionUri;
public AzureFunctionCaller(string functionName)
{
_httpClient = new HttpClient();
_httpClient.DefaultRequestHeaders.Add("x-functions-key","<Key>");
_functionUri = $"<FUNCTION_ENDPOINT>/admin/functions/{functionName}";
}
public async Task CallViaAdminEndpoint(string content)
{
var httpContent = new StringContent(content, Encoding.UTF8, "application/json");
var response = await _httpClient.PostAsync(_functionUri, httpContent);
var responseContent = await response.Content.ReadAsStringAsync();
Console.WriteLine($"Response content: {responseContent}");
}
}
Then you must send the data in a format where you place the content in "input" object.
var azureFunctionCaller = new AzureFunctionCaller("<FunctionName>");
var obj = new
{
... // properties you want to send
};
var jsonContent = JsonConvert.SerializeObject(new
{
input = JsonConvert.SerializeObject(obj)
});
await azureFunctionCaller.CallViaAdminEndpoint(jsonContent);`
To explain the input property, here is how the same call looks like in postman:
I'm writing queue trigger function where I read data from queue, and send them another web service using RESTFul service. Right now, I'm testing a very simple REST api call that I only need to provide token in the header and expect very simple JSON response from the server. The JSON just contains an email address entry and that's about it. My understanding is that if I read response asynchronously, I would need to change function prototype to comply with async call. But that's not possible in Azure function app. So what's the best way to read JSON response object?
This is my attempt so far :
using System;
using System.Net.Http;
using System.Net.Http.Headers;
public static void Run(string myQueueItem, TraceWriter log)
{
string URL = "https://api.spotlightessentials.com/api/v2/user";
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(URL);
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("token","<Token value>");
HttpResponseMessage response = client.GetAsync("").Result;
if (response.IsSuccessStatusCode)
{
// How do I read Json response here
}
}
else
{
Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
}
}
In your
if (response.IsSuccessStatusCode)
you can do this:
var responseData = await response.Content.ReadAsAsync<YourObjectTypeHere>();
Or you could also do something like this depending on your needs:
var responseData = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
if (!string.IsNullOrWhiteSpace(responseData))
{
var responseDataObject =
JsonConvert.DeserializeObject<YourObjectTypeHere>(responseData);
}
Or a combination of parts of the 2.