Web api azure 400 Bad Request

Ángel Rubén Valdeolmos Carmona 586 Reputation points
2022-03-18T14:31:52.947+00:00

A strange thing happens to me, I have published an api with swagger, in a post method I have a body with some properties that are a string, in my api I check if one of them is null, in my local everything works fine, both in swagger and postman, but when I publish the api, setting the first parameter to null gives me a 400 Bad Request.

I leave the code of the controller

  [HttpPost]
        public async Task<IActionResult> Message([FromBody]MessageFirebaseRequest messageFirebaseRequest)
        {
            var firebaseMessaging = FirebaseMessaging.GetMessaging(_firebaseApp);

            var data = new Dictionary<string, string>
            {
                { "body", messageFirebaseRequest.Message },
                { "alert", messageFirebaseRequest.Alert.ToString() },    
                { "priority", "high" },
                { "other_key", "true" }
            };

            if (messageFirebaseRequest.Silent)
            {
                data.Add("silent", "true");
            }

            var message = new Message
            {
                Data = data,
                Android = new AndroidConfig
                {
                    Priority = Priority.High
                }
            };

            if (messageFirebaseRequest.Topic is null)
            {
                message.Token = messageFirebaseRequest.Token;
            }
            else
            {
                message.Topic = messageFirebaseRequest.Topic;
            }

            string result = await firebaseMessaging.SendAsync(message);

            return Ok(result);
        }
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,177 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
6,901 questions
{count} votes

2 answers

Sort by: Most helpful
  1. AgaveJoe 26,136 Reputation points
    2022-03-19T11:30:45.773+00:00

    I'm guessing the topic property is not configured as a nullable type.

        public class MessageFirebaseRequest
        {
            public string? topic { get; set; }
            public string? token { get; set; }
            public string? message { get; set; }
            public bool alert { get; set; }
            public bool silent { get; set; }
    
        }
    

  2. Ángel Rubén Valdeolmos Carmona 586 Reputation points
    2022-04-08T04:50:08.63+00:00

    The problem was that the class implements an interface with topic, message and token, by putting these properties in the class itself the problem disappeared, but it only happens when I deploy in azure, something very curious.

    0 comments No comments