简体   繁体   中英

BadRequest when PUT JSON data on Firebase REST API

I have this code that I am using to PUT my JSON string on Firebase database:

RestRequest request = new RestRequest("MemberAndChannels/{userId}/{channelId}.json", Method.POST);

request.RequestFormat = DataFormat.Json;
request.AddParameter("auth", accessKey);
request.AddUrlSegment("userId", user.UUID);
request.AddUrlSegment("channelId", channel.UUID);
request.AddHeader("Content-Type", "application/json; charset=utf-8");

request.AddJsonBody(channelJson);

IRestResponse response = client.Execute(request);

if (response.StatusCode == HttpStatusCode.OK)
{

} 
else {

}

But I am getting following error (StatusCode: BadRequest):

"{\n  \"error\" : \"Invalid data; couldn't parse JSON object, array, or value.\"\n}\n"

I've tried PUTing same data using curl and it worked. Can't figure out where I am doing wrong.

The object is being serialized twice (double serialized).

Pass the channel object as it is to AddJsonBody and the request will serialize it to JSON before sending body

request.AddJsonBody(channel);

assuming here channel is an object of a Class

I have modified the code with slight changes. Please try with this

RestRequest request = new RestRequest("MemberAndChannels/{userId}/{channelId}.json", Method.POST);
request.AddParameter("auth", accessKey); // or request.AddHeader("auth", accessKey);
request.AddUrlSegment("userId", user.UUID);
request.AddUrlSegment("channelId", channel.UUID);

request.AddHeader("Content-Type", "application/json");
request.AddHeader("Accept", "application/json");
request.AddParameter("application/json", channelJson, ParameterType.RequestBody);

IRestResponse response = client.Execute(request);

if (response.StatusCode == HttpStatusCode.OK)
{

} 
else {

}

I had the same problem.

I soved it changing this code:

request.AddParameter("auth", accessKey);

by

request.AddQueryParameter("auth", accessKey);

I hope help you.

Regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM