如何在azure函数中访问http请求的所有字段(在C#中解析JSON)



microsoft azure对我来说是一个全新的编程主题。编程基础语言是C#。我必须使用来自逻辑应用程序的Azure Funtion Http触发器(当新电子邮件到达时(。我将日志应用程序中传入电子邮件的所有可能数据提供给到azure函数调用。

然后我首先遇到了问题,HttpRequestreq中没有任何内容(没有数据(是可请求的。

我发现C#.NetCore3.1在JSON对象方面有问题,并返回了null对象。然后我用.NetCore2.1构建了这个项目,它基本上起作用了。我得到了正确的信息,比如req.Host,在这样的调用之后,我在数据中看到了一个无效的JSON对象

string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);

但我不知道如何解析JSON对象中的数据(from,to,subject,content,attachment,..(。

我尝试了很多我发现的样本,但我不喜欢有效的,例如get null、exception,。。

请有人在这里发布一个适用于使用VS2017/2019.NetCore2.1.编译的azure函数的小代码片段

还有一个问题是,最终是否有办法直接从http请求中获取MimeMessage obj?然后我可以直接从该对象获得信息,但可以帮助我解决这两个问题哪里好。

否则,我会把JSoN结构泄漏到自己身上,以一种非常尴尬的方式获取信息。所以我希望有人能帮助我。

public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation($" host   : {req.Host}");
string name = req.Query["name"];
// read the contents of the posted data into a string
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
// use Json.NET to deserialize the posted JSON into a C# dynamic object
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;

例如,我有这样一个Json内容(在这2行代码之后的dtaa中(:

string requestBody=等待新的StreamReader(req.Body(.ReadToEndAsync((;动态数据=JsonConvert.DescializeObject(requestBody(;

Json用于解析短电子邮件的内容(其他请求可能会在或多或少的参数(字段(中有所不同(

{ {
"Conversion Id": "AAQkADRmZDAwMTMwLTI3MTMtNGI0Ny1iMzFiLTQzYWJiZDY0YWI1ZQAQAE2OZM06t0uQqpd9MuONKNQ=",
"From": "test1@testxyz.com",
"Has Attachment": false,
"Internet Message Id": "<AM0PR07MB44178000A0B37882D6BC01D99ACC0@AM0PR07MB4417.eurprd07.prod.outlook.com>",
"Is Html": false,
"Is rRad": false,
"Message Id": "AAMkADRmZDAwMTMwLTI3MTMtNGI0Ny1iMzFiLTQzYWJiZDY0YWI1ZQBGAAAAAAAvlJZPJxnGS4f6YWxh7zGsBwBnLziSnuG8R6h5C2SVmlYlAAHViSWpAACAWl3JfUo4SI7D5g-MgfEiAAJiPJQeAAA=",
"Received Time": "2020-12-09T14:05:06+00:00",
"Subject": "test1",
"To": "test2@testxyz.com",
"emailBody": "1234rn",
"importance": "normal"
}
}

对于这个需求,您需要在函数代码中创建一个内部类来将请求体解析为对象。请参阅我下面的代码:

using System;
using System.IO;
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;
namespace FunctionApp2
{
public static class Function1
{
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
//dynamic data = JsonConvert.DeserializeObject(requestBody);
EmailItem data = JsonConvert.DeserializeObject<EmailItem>(requestBody);
log.LogInformation(data.From);
log.LogInformation(data.Has_Attachment.ToString());
return new OkObjectResult("completed");
}
}
public class EmailItem
{
public string From { get; set; }
public Boolean Has_Attachment { get; set; }
//Add other fields which you want, please note add all of the fields which your request may contains in this inner class.
}
}

正如您提到的other request could differn in more ore lesser paramters(fields),因此请在EmailItem类中添加您的请求可能包含的所有字段。

顺便说一句,我上面提供的代码是根据您的需要用.net核心2.1进行测试的。但我认为该代码也可以在.net core 3.1 中工作

最新更新