![](/img/trans.png)
[英]Integrating Cosmos DB with OData using Azure Http trigger function app
[英]Azure Function HTTP trigger to receive nested JSON and store in Cosmos DB
I am attempting to create an Azure Function HTTP trigger that will take the received nested JSON data and store into Cosmos DB using POST. 我创建了一个 function ,它将存储没有嵌套的 JSON,但不明白如何处理嵌套部分。
代码示例(run.csx):
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
public static IActionResult Run(HttpRequest req, out object testDocument, ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
dynamic body = await req.Content.ReadAsStringAsync();
var e = JsonConvert.DeserializeObject < Root > (body as string);
return req.CreateResponse(HttpStatusCode.OK, e);
}
public class Vehicles {
public string car {
get;
set;
}
public string bike {
get;
set;
}
public string plane {
get;
set;
}
}
public class Root {
public string name {
get;
set;
}
public int age {
get;
set;
}
public Vehicles vehicles {
get;
set;
}
}
}
if (!string.IsNullOrEmpty(Category) && !string.IsNullOrEmpty(Label)) {
testDocument = new {
name,
age,
vehicles,
car,
bike,
plane
}
return (ActionResult) new OkResult();
} else {
testDocument = null;
return (ActionResult) new BadRequestResult();
}
}
绑定(function.json):
{
"bindings": [
{
"authLevel": "anonymous",
"name": "req",
"type": "httpTrigger",
"direction": "in",
"methods": [
"get",
"post"
]
},
{
"name": "$return",
"type": "http",
"direction": "out"
},
{
"name": "testDocument",
"direction": "out",
"type": "cosmosDB",
"connectionStringSetting": "XXXX-test_DOCUMENTDB",
"databaseName": "testDatabase",
"collectionName": "testCollection",
"createIfNotExists": true
}
]
}
输入:
{
"name":"Ram",
"age":27,
"vehicles": {
"car":"limousine",
"bike":"ktm-duke",
"plane":"lufthansa"
}
}
此代码在运行时会产生 500 Internal Server Error。
如果有人能指出错误在哪里,或者它的结构是否正确,我将不胜感激。
先感谢您。
该错误是因为您的 class 结构不完整。 看起来您已经从两个不同的代码文件中剪切了粘贴。 这与Root
class 具有嵌套的 class 属性这一事实无关。
它有助于创建一个 C# function 项目,如果您想坚持纯粹在浏览器中执行此操作,您可以在本地进行调试。
如果你注释掉错误的代码,它应该编译:
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
public static IActionResult Run(HttpRequest req, out object testDocument, ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
dynamic body = await req.Content.ReadAsStringAsync();
var e = JsonConvert.DeserializeObject<Root>(body as string);
return req.CreateResponse(HttpStatusCode.OK, e);
}
public class Vehicles
{
public string car
{
get;
set;
}
public string bike
{
get;
set;
}
public string plane
{
get;
set;
}
}
public class Root
{
public string name
{
get;
set;
}
public int age
{
get;
set;
}
public Vehicles vehicles
{
get;
set;
}
}
// }
// if (!string.IsNullOrEmpty(Category) && !string.IsNullOrEmpty(Label))
// {
// testDocument = new
// {
// name,
// age,
// vehicles,
// car,
// bike,
// plane
// }
//
//
// return (ActionResult)new OkResult();
// }
// else
// {
// testDocument = null;
// return (ActionResult)new BadRequestResult();
// }
//}
但是,您会看到这对您的 CosmosDB 逻辑没有任何作用。
另外,请不要滥用你在那里的方式dynamic
,来自ReadAsStringAsync
的响应是一个Task<string>
,只需使用它:
string body = await req.Content.ReadAsStringAsync();
var e = JsonConvert.DeserializeObject<Root>(body);
return req.CreateResponse(HttpStatusCode.OK, e);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.