![](/img/trans.png)
[英]HttpRequest object is null in response from Azure Function
[英]How to convert an Azure Function HttpRequest body to an object
我在经典的 web apis 中做过几次,但在 Azure 函数中没有,所以我不确定我在这里缺少什么:
我的实体用户:
[SharedCosmosCollection("shared")]
public class User : ISharedCosmosEntity
{
/// <summary>
/// User id
/// </summary>
[JsonProperty("Id")]
public string Id { get; set; }
/// <summary>
/// Cosmos entity name for shared collection
/// </summary>
[CosmosPartitionKey]
public string CosmosEntityName { get; set; }
public string GivenName { get; set; }
public string FamilyName { get; set; }
public string NickName { get; set; }
public string Name { get; set; }
public string Picture { get; set; }
public string Locale { get; set; }
public DateTime UodatedAt { get; set; }
public string Email { get; set; }
public bool EmailVerified { get; set; }
public string Sub { get; set; }
}
我的 CreateUser 的 Function 代码:
[FunctionName("CreateUser")]
public static async Task<IActionResult> CreateUser(
[HttpTrigger(AuthorizationLevel.Function,
"post", Route = "user")]
HttpRequest req)
{
var telemetry = new TelemetryClient();
try
{
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var input = JsonConvert.DeserializeObject<User>(requestBody);
var userStore = CosmosStoreHolder.Instance.CosmosStoreUsers;
var added = await userStore.AddAsync(input);
return new OkObjectResult(added);
}
catch (Exception ex)
{
string guid = Guid.NewGuid().ToString();
var dt = new Dictionary<string, string>
{
{ "Error Lulo: ", guid }
};
telemetry.TrackException(ex, dt);
return new BadRequestResult();
}
}
在门户中,我在请求正文中发送此 JSON
{
"given_name": "aaa",
"family_name": "bbb",
"nickname": "xx.xx.psg",
"name": "bbbb",
"picture": "https://lh3.googleusercontent.com/a-/AOhsGg8qaBLDPubSaNb3u8zMyiUGrwFE3zhQ8MMqLALjGc",
"locale": "es",
"updated_at": "2020-04-20T15:33:16.133Z",
"email": "xx.xx.psg@gmail.com",
"email_verified": true,
"sub": "google-oauth2|1111"
}
但是我收到 http 500 错误,不确定是什么问题
如果 class 属性与 Z0ECD10F1C1D7ABBD784A 属性不完全匹配,则序列化程序无法在没有一点帮助的情况下将 JSON 属性与 class 属性匹配。 对于 map JSON 属性到 class 属性,请为每个适用属性使用JsonProperty
属性。 这将适用于序列化和反序列化。
[JsonProperty("given_name")]
public string GivenName { get; set; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.