Firebase REST API上的PUT JSON数据时的BadRequest

时间:2018-06-02 15:48:37

标签: c# asp.net firebase-realtime-database restsharp

我有这个代码用于在Firebase数据库上输出我的JSON字符串:

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 {

}

但是我收到了以下错误(StatusCode:BadRequest):

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

我尝试使用curl尝试使用相同的数据并且它有效。无法弄清楚我做错了什么。

3 个答案:

答案 0 :(得分:0)

对象被序列化两次(双序列化)。

channel对象原样传递给AddJsonBodyrequest会将其序列化为JSON,然后再发送正文

request.AddJsonBody(channel);

假设此处channel是Class

的对象

答案 1 :(得分:0)

我稍微修改了代码。请尝试使用此

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 {

}

答案 2 :(得分:0)

我遇到了同样的问题。

我发现它改变了这段代码:

request.AddParameter("auth", accessKey);

通过

request.AddQueryParameter("auth", accessKey);

我希望能帮助你。

此致