简体   繁体   中英

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:

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:

[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

{
  "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

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. To map JSON property to a class property, use the JsonProperty attribute for each applicable property. This will work for both serialization and deserialization.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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