Alk
Alk

Reputation: 5557

Firebase POST Request - REST API

I'm trying to submit a POST reqeust to Firebase using the REST API. Following the API, I created the following JSON which I submit as the body of my request:

"'{\"senderId\":\"CBPA9tdrNyc1Y3AxSb8rkZjDJxh2\",\"senderName\":\"John\",\"text\":\"Hello its me\"}'"

This is how my data structure looks:

enter image description here

However, after submitting my request, instead of creating an entry similar to the one above, the whole string gets added as a single entry as seen below:

enter image description here

How can I fix this?

This is the curl request suggested by the Firebase Documentation:

curl -X POST -d '{"user_id" : "jack", "text" : "Ahoy!"}' \
  'https://[PROJECT_ID].firebaseio.com/message_list.json'

I am currently testing this using https://www.hurl.it/ with the following request:

enter image description here

Upvotes: 1

Views: 9664

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599541

If you carefully compare your JSON to the one from the documentation, you have an extra " before and after the data. This means that you're posting one big string, instead of a JSON object.

The solution is to remove the extra double quotes from start and end. That also means that you no longer needs to escape the double quotes inside the JSON:

'{"senderId":"CBPA9tdrNyc1Y3AxSb8rkZjDJxh2","senderName":"John","text":"Hello its me"}'

The single quotes are needed if the tool you use to post tries to parse the JSON as a single string. If it doesn't (hint: hurl.it doesn't), you should post the body without the surrounding ' too.

Request:

HTTP POST request

Response:

HTTP response

Upvotes: 3

Related Questions