Hai*_*OUI 6 c# httpclient azure azure-functions
我有两个运行良好的 blob 触发的 Azure 函数(一个是,v2
另一个v1
)。另一方面,我在我的 Azure Devops 中发布了一个 REST Web API 应用程序(公开了加密和解密流的方法)(仍未部署在 Azure 门户上,实际上,仅将代码添加到 Azure Devops 存储库中)。
我想要做的是通过 HTTP 调用(调用加密或解密或其他方式)从我的 Azure 函数调用 Web API 应用程序来解密 blob 内容。
无需身份验证。
按照最佳实践的顺序,从我的 Web API 制作 API 应用程序更合适,还是只是将我的 Web API 项目作为 Web 应用程序部署到 Azure 更合适?为什么?
换句话说,从 Azure 函数调用 API 的最佳方式是什么?
谁能给我提供一些代码示例吗?
看来你想在API
你的内部调用azure function
这里是代码示例以供你理解:
在此函数中,我提供了一个 MPN 编号作为输入,该编号从 a 有效3rd party API
并返回true
并false
作为响应
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Net.Http;
using System.Net;
using System.Text;
namespace HaithemKAROUIApiCase.Functions
{
public static class HaithemKAROUIApiCaseClass
{
[FunctionName("HaithemKAROUIApiCaseFunction")]
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequestMessage req, ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
try
{
// Convert all request param into Json object
var content = req.Content;
string jsonContent = content.ReadAsStringAsync().Result;
dynamic requestPram = JsonConvert.DeserializeObject<PartnerMpnModel>(jsonContent);
// Extract each param
//string mpnId = requestPram.mpnId;
if (string.IsNullOrEmpty(requestPram.MpnID))
{
return req.CreateResponse(HttpStatusCode.OK, "Please enter the valid partner Mpn Id!");
}
// Call Your API
HttpClient newClient = new HttpClient();
HttpRequestMessage newRequest = new HttpRequestMessage(HttpMethod.Get, string.Format("YourAPIURL?mpnId={0}", requestPram.MpnID));
//Read Server Response
HttpResponseMessage response = await newClient.SendAsync(newRequest);
bool isValidMpn = await response.Content.ReadAsAsync<bool>();
//Return Mpn status
return req.CreateResponse(HttpStatusCode.OK, new PartnerMpnResponseModel { isValidMpn = isValidMpn });
}
catch (Exception ex)
{
return req.CreateResponse(HttpStatusCode.OK, "Invaild MPN Number! Reason: {0}", string.Format(ex.Message));
}
}
}
public class PartnerMpnModel
{
public string MpnID { get; set; }
}
public class PartnerMpnResponseModel
{
public bool isValidMpn { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
请求格式
{
"MpnID": "123456789"
}
Run Code Online (Sandbox Code Playgroud)
如果您还有任何疑问,请随时分享。谢谢,编码愉快!
归档时间: |
|
查看次数: |
45773 次 |
最近记录: |