【问题标题】:How to convert an Azure Function HttpRequest body to an object如何将 Azure Function HttpRequest 正文转换为对象
【发布时间】:2020-08-05 17:20:21
【问题描述】:

我在经典的 web apis 中做过几次,但在 Azure Functions 中没有,所以我不确定我在这里缺少什么:

我的实体用户:

[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 函数代码:

[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 错误,但不确定是什么问题

【问题讨论】:

    标签: c# .net azure asp.net-core azure-functions


    【解决方案1】:

    如果类属性与 JSON 属性不完全匹配,则序列化程序无法在没有一点帮助的情况下将 JSON 属性与类属性匹配。要将 JSON 属性映射到类属性,请为每个适用的属性使用 JsonProperty 属性。这适用于序列化和反序列化。

    [JsonProperty("given_name")]
    public string GivenName { get; set; }
    

    【讨论】:

      猜你喜欢
      • 2013-02-22
      • 2010-11-29
      • 1970-01-01
      • 2013-04-22
      • 2021-10-14
      • 1970-01-01
      • 2017-09-17
      • 2018-10-04
      相关资源
      最近更新 更多