question

Angelru-4290 avatar image
0 Votes"
Angelru-4290 asked Angelru-4290 answered

Web api azure 400 Bad Request

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);
         }
dotnet-aspnet-core-webapiazure-webapps-apis
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

A 400 is a client error which usually means the request is malformed. You did not share the request or the input parameter so we have no idea what could be wrong. Please debug the client.

0 Votes 0 ·

this same code works pointing against my local

 {
       "topic": null,
       "token": "token",
       "message": "hello",
       "alert": true,
       "silent": true
  }


0 Votes 0 ·

@Angelru-4290 you're getting a 400 only when topic is set to null? What is topic set to when it's successful? Have set ASPNETCORE_ENVIRONMENT to development so you can see where inside the method is throwing the exception?

0 Votes 0 ·
AgaveJoe avatar image
0 Votes"
AgaveJoe answered ryanchill edited

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


· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The problem is that if you run the api locally it works fine, it only happens when you deploy to Azure.

0 Votes 0 ·

The problem is that if you run the api locally it works fine, it only happens when you deploy to Azure.

A 400 error means there is a problem with the client request. If the topic property of the MessageFirebaseRequest class is not a nullable type but a null is passed then a 400 (Bad Request) is the expected status response in ASP.NET Core 6.0. Did you at least try a nullable type???

Keep in mind, the community cannot see your development environment or your testing procedures. Perhaps you are targeting Core 5 or 3.1 on your development system but on Azure .NET 6.0 is installed.







0 Votes 0 ·

this also fails

  {
        "topic": "",
        "token": "token",
        "message": "hello",
        "alert": true,
        "silent": true
   }

If I change in my controller and remove the null check and put for example

  if (messageFirebaseRequest.Topic is "string")

this also fails

This works

 {
            "topic": "topic",
            "token": null,
            "message": "hello",
            "alert": true,
            "silent": true
 }

I am running everything on net 6.0

0 Votes 0 ·
Show more comments
Angelru-4290 avatar image
0 Votes"
Angelru-4290 answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.