Akshay Gupta
Akshay Gupta

Reputation: 31

FCM Sending HTTP Post request gives error code 400()

I am using HTTP POST request to give notification to authors on my web-app that their stories are approved by our admin.

My payload looks like this

var payload = {
  "notification": {
    "title": "Story Approved!",
    "body": "Your story is approved"
  },
  "to" : to
}

This is my request

$.ajax({
  type: 'POST',
  url: 'https://fcm.googleapis.com/fcm/send',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'key='+serverKey
  },
  data: payload,
  success: function(response){
    console.log(response);
  },
});

Whenever I try to send the request it gives me the following error

POST https://fcm.googleapis.com/fcm/send 400 ()

I am kinda new to this and according to Firebase Error Response Codes

Invalid JSON 400 Check that the JSON message is properly formatted and contains valid fields (for instance, making sure the right data type is passed in).

But I cannot figure out where I am wrong. Any help would be appreciated

Upvotes: 0

Views: 3437

Answers (1)

mohsen solhnia
mohsen solhnia

Reputation: 376

just convert your payload to json like this:

 data: JSON.stringify(msg)

and your code will be like this:

$.ajax({
  type: 'POST',
  url: 'https://fcm.googleapis.com/fcm/send',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'key='+serverKey
  },
  data: JSON.stringify(msg),
  success: function(response){
    console.log(response);
  },
});

Upvotes: 1

Related Questions