Search code examples
c#jsonapidropbox-api

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());

Im getting the following error: Error in call to API function "sharing/create_shared_link_with_settings": request body: could not decode input as JSON. I am using the dropbox api.

I saw the following: https://github.com/csharp-leaf/Leaf.xNet/issues/66 (Someone had a similar issue, but this fix did not work)


Solution

  • When you are using RequestParams that means Context-Type: application/x-www-form-urlencoded.
    You can't set Content-Type via .AddHeader(), use argument for .Post(url, json, contentType) method like this:

    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);