Leaf.XNet C# 将正文发送为 Json 无效

Leaf.XNet C# Sending Body as Json not working

HttpRequest httpRequest = new HttpRequest();
RequestParams reqParams = new RequestParams { };
httpRequest.IgnoreProtocolErrors = true;

reqParams["data"] = "{\"path\": \"/Prime_Numbers.txt\"}";
httpRequest.AddHeader("Authorization", " Bearer MYKEY");
httpRequest.AddHeader("Content-Type", "application/json");

Console.WriteLine(httpRequest.Post("https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings", reqParams).ToString());

我收到以下错误:调用 API 函数“sharing/create_shared_link_with_settings”时出错:请求正文:无法将输入解码为 JSON。我正在使用保管箱 api.

我看到了以下内容:https://github.com/csharp-leaf/Leaf.xNet/issues/66(有人遇到过类似的问题,但此修复无效)

当您使用 RequestParams 时,这意味着 Context-Type: application/x-www-form-urlencoded
您不能通过 .AddHeader() 设置 Content-Type,使用 .Post(url, json, contentType) 方法的参数,如下所示:

HttpRequest httpRequest = new HttpRequest();
httpRequest.AddHeader("Authorization", " Bearer MYKEY");

string url = "https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings";
string jsonData = "{\"path\": \"/Prime_Numbers.txt\"}";
string response = httpRequest.Post(url, jsonData, "application/json").ToString();

Console.WriteLine(response);