Firebase REST POST request with an error: "Bad data, could not parse object, array or JSON value ..."

I am trying to use the Firebase REST API to store data in my data warehouse. I tried with jQuery and vanilla JS XHR. However, both give the same error. 403 Bad request and this answer:

Invalid data failed to parse JSON object, array or value. You may be using invalid characters in your key names.

Here is my JSON example, I'm trying to save:

{ "date": "2pm", "name": "John" } 

Here is an example ajax request:

 jQuery.ajax({ accept: "application/json", type: 'POST', contentType: "application/json; charset=utf-8", dataType: "json", url: "https://something.firebaseio.com/endpointnode.json", data: { "name": "John", "date": "2pm" }, }); 

Response to the request:

 { "error" : "Invalid data; couldn't parse JSON object, array, or value. Perhaps you're using invalid characters in your key names." } 

As you can see, there are no special characters or anything else. It should just work.

It works great with CURL and Httpie. I tried checking the -v in Httpie for details. I put all the headers like Httpie. Nothing helped. By the way, my environment is writable, so there should not be any resolution problems.

Any idea how to achieve this?

Thanks.

+6
source share
2 answers

You indicated that your AJAX request contains a json string by contentType property. However, parameters attached to the request are not a JSON string. To make the data as a json string, simply call the JSON.stringify(params) method.

The following code snippet will help you solve the problem.

 var data = {"name": "John", "date": "2pm"}; jQuery.ajax({ accept: "application/json", type: 'POST', contentType: "application/json; charset=utf-8", dataType: "json", url: "https://something.firebaseio.com/endpointnode.json", data: JSON.stringify(data), }); 

Greetings.

+9
source

Your data should be a string, use JSON.stringify to convert your object to a string:

 data: JSON.stringify({"name": "John", "date": "2pm"}) 
+2
source

Source: https://habr.com/ru/post/988996/


All Articles