我想将参数传递给Azure函数2中的HttpTrigger类型。我在下面的链接中看到Azure函数1的答案。
How to pass parameters by POST to an Azure function?
以上链接中的答案为File cacheDirectory = context.getCacheDir(); //App crashes here since there is no external storage
String cachePath = "";
if(cacheDirectory != null){
cachePath = cacheDirectory.toPath().normalize().toString();
}
我正在寻找与Azure Function 2类似的答案。
在这里,我们使用的是HttpRequest类而不是HttpRequestMessage类。
下面是代码。
await req.Content.ReadAsAsync<>();
答案 0 :(得分:0)
public static async void Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)]
HttpRequest req,
ILogger log)
{
var content = await new StreamReader(req.Body).ReadToEndAsync();
MyClass myClass = JsonConvert.DeserializeObject<MyClass>(content);
}
您可以拥有一个自定义类,该类可以从发布请求中以JSON的形式发送。
答案 1 :(得分:0)
似乎您正在尝试向POST
发送Azure Function V2
请求。请参见下面的代码段。
自定义请求类别:
public class Users
{
public string Name { get; set; }
public string Email { get; set; }
}
Azure Function V2:
在此示例中,我使用自定义类获取两个参数,并返回该两个类属性作为响应。
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.");
//Read Request Body
var content = await new StreamReader(req.Body).ReadToEndAsync();
//Extract Request Body and Parse To Class
Users objUsers = JsonConvert.DeserializeObject<Users>(content);
//As we have to return IAction Type So converting to IAction Class Using OkObjectResult We Even Can Use OkResult
var result = new OkObjectResult(objUsers);
return (IActionResult)result;
}
索取样品:
{
"Name": "Kiron" ,
"Email": "kiron@email.com"
}
PostMan测试:
注意: 您实际上是在寻找
await req.Content.ReadAsAsync<>();
从您的函数发送POST
请求中需要。并从中读取 该服务器的响应。但是请记住,req.Content
不支持Azure Function V2
作为阅读帖子的请求,它已经显示了Function V1
示例here
另一个示例:
请参阅以下代码段:
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.");
//Read Request Body
var content = await new StreamReader(req.Body).ReadToEndAsync();
//Extract Request Body and Parse To Class
Users objUsers = JsonConvert.DeserializeObject<Users>(content);
//Post Reuqest to another API
HttpClient client = new HttpClient();
var json = JsonConvert.SerializeObject(objUsers);
//Parsing json to post request content
var stringContent = new StringContent(json, UnicodeEncoding.UTF8, "application/json");
//Posting data to remote API
HttpResponseMessage responseFromApi = await client.PostAsync("YourRequstURL", stringContent);
//Variable for next use to bind remote API response
var remoteApiResponse = "";
if (responseFromApi.IsSuccessStatusCode)
{
remoteApiResponse = responseFromApi.Content.ReadAsStringAsync().Result; // According to your sample, When you read from server response
}
//As we have to return IAction Type So converting to IAction Class Using OkObjectResult We Even Can Use OkResult
var result = new OkObjectResult(remoteApiResponse);
return (IActionResult)result;
}
希望您能理解,如果您还有任何疑问,请随时与我们分享。谢谢,祝您编程愉快!
答案 2 :(得分:0)
首先,基于HTTP请求创建Azure Function Project(或添加到您的解决方案中)。触发。在创建的项目的单个类中,您可以看到示例。您从请求正文传递了JSON:
{"name":"John"}
,然后将其转换为代码中的变量:
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 requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
string name = data.name;
...