简体   繁体   English

如何将 Azure Function HttpRequest 正文转换为 object

[英]How to convert an Azure Function HttpRequest body to an object

I have done this several times in the classic web apis, but not in Azure Functions, so I am not sure what I am missing here:我在经典的 web apis 中做过几次,但在 Azure 函数中没有,所以我不确定我在这里缺少什么:

My entity User:我的实体用户:

[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; }

    }

My Function code for CreateUser:我的 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();
            }
        }  

and in the portal, I am sending this JSON in the request body在门户中,我在请求正文中发送此 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"
}

However I am getting an http 500 error and not sure what is the issue但是我收到 http 500 错误,不确定是什么问题

The serializer cannot match the JSON properties with the class properties without a little help if the class properties do not exactly match the JSON properties.如果 class 属性与 Z0ECD10F1C1D7ABBD784A 属性不完全匹配,则序列化程序无法在没有一点帮助的情况下将 JSON 属性与 class 属性匹配。 To map JSON property to a class property, use the JsonProperty attribute for each applicable property.对于 map JSON 属性到 class 属性,请为每个适用属性使用JsonProperty属性。 This will work for both serialization and deserialization.这将适用于序列化和反序列化。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM