cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
What’s new: end-to-end encryption, Replay and Dash updates. Find out more about these updates, new features and more here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Not getting response when I call "2/files/listfolder" method

Not getting response when I call "2/files/listfolder" method

Shilpa M.
New member | Level 1

Hi

I passed "https://api.dropboxapi.com/2/files/list_folder" to fetch files and folder from root.

and Passed parameters are:-

string path=string.empty;

request.AddHeader("Content-Type","application/json");

request.AddParameter("path", path);

but I got response from api is folollowing:

Error in call to API function "files/list_folder": Bad HTTP "Content-Type" header: "application/x-www-form-urlencoded".  Expecting one of "application/json", "application/json; charset=utf-8", "text/plain; charset=dropbox-cors-hack".

and StatusDescription "BadRequest"

 

Could you please suggest me?

Thanks

15 Replies 15

Greg-DB
Dropbox Staff

The error response is indicating that the call supplied an unexpected Content-Type header value. Specifically, the server received "application/x-www-form-urlencoded", when it only expects one of "application/json", "application/json; charset=utf-8", or "text/plain; charset=dropbox-cors-hack".

It looks like you are trying to set "application/json" via this line though:

request.AddHeader("Content-Type","application/json");

That being the case, it sounds like your HTTP client is overriding that. I don't know what client you're using, but perhaps that's happening because you're then using that AddParameter method? If that is adding the path value as a form variable, the client may be automatically changing the Content-Type to match.

Note that parameters on Dropbox API v2 aren't supposed to be sent as encoded form variables anyway, but rather as JSON, as shown in the documentation:

https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder

Shilpa M.
New member | Level 1

Hi

I am using RestClient.

HTTP client is not overriding at my side. I got authorize and token successfully.

I am using following code for get list of folders:

RestClient restClient = new RestClient("https://api.dropboxapi.com");

private IRestResponse executeRequest(RestRequest request)
{
            var iy = GetTokenInfo(ref status, ref errMsg);
            if (status == "OK" && errMsg == ")
            {
                restClient.Authenticator = new HttpBasicAuthenticator(clientId, clientSecret);
            }
          
            request.AddHeader("Authorization", "Bearer " + accessToken);
            request.AddHeader("Content-Type", "application/json");
            return restClient.Execute(request);
 }

 

  private RestRequest FormRequest(RestType rtype, string data, Boolean useXML)
        {
            Method method;
            switch (rtype)
            {
                case RestType.Get:
                    method = Method.GET;
                    break;
                case RestType.Post:
                    method = Method.POST;
                    break;
                case RestType.Put:
                    method = Method.PUT;
                    break;
                case RestType.Delete:
                    method = Method.DELETE;
                    break;

                default: return null;
            }

            RestRequest request = new RestRequest(data, method);

            if (useXML)
                request.AddHeader("Accept", "application/xml");
            else
                request.AddHeader("Accept", "application/json");

            return request;
        }

And list of folders:-

 public string GetMetaData(string path, ref string status, ref string errMsg)
        {
            string responseStr = ";
            string url = "/2/files/list_folder";
            RestType type = RestType.Post;
            Boolean useXml = false;
            RestRequest request = FormRequest(type, url, useXml);
            request.AddParameter("path", path);
            //request.AddParameter("include_media_info", false);
            //request.AddParameter("include_deleted", false);
            IRestResponse response;
            try
            {
                response = executeRequest(request);
                responseStr = response.Content;
                status = response.StatusDescription;
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
                return null;
            }
            return responseStr;
        }

 

And again I got same error as I sent to yesterday.

Please suggest me.

Greg-DB
Dropbox Staff

What makes you think your HTTP client isn't overriding the Content-Type, and how are you checking that? That error message from the server just echoes back the Content-Type it received.

Note that the OAuth endpoints work differently from the other API endpoints, so those aren't a good comparison.

You are using something called "FormRequest", but parameters for API v2 calls such as /files/list_folder and /files/get_metadata shouldn't be form encoded.

Greg-DB
Dropbox Staff

Specifically, when using RestClient with API v2 calls, it looks like you should use .AddJsonBody instead of .AddParameter. 

Shilpa M.
New member | Level 1

Last issue was fixed. Thanks for response.

Today I got one issue. When I upload file. I am using following

"https://content.dropboxapi.com/2/files/upload"

And Please look my code:

RestClient client = new RestClient("https://content.dropboxapi.com");

 private IRestResponse executeRequestContent(RestRequest request)
 {
            request.AddHeader("Authorization", "Bearer " + accessToken);
            return client.Execute(request);
}

 public string UploadDocument(ExternalFile externalFile, MetadataJson metadataJson, ref string status, ref string errMsg)
        {
            string data = String.Format("/2/files/upload");
            string responseStr = ";

            RestRequest request = FormRequest(Method.POST, data, true);
            IRestResponse response = new RestSharp.RestResponse();

            request.Timeout = 1000 * 60 * 10;
            try
            {
                string fullPathToUpload = metadataJson.path + "/" + externalFile.Name;
                string path = string.Format("{1}\"path\":\"{0}\"{2}", fullPathToUpload, "{", "}");
                request.AddHeader("Dropbox-API-Arg", path);
                request.AddHeader("Content-Type", "application/octet-stream");
                request.AddFile("file", externalFile.FileBody, externalFile.Name, externalFile.ContentType);
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
            }
            response = executeRequestContent(request);
            responseStr = response.Content;
            responseStr = response.StatusDescription;
            return responseStr;
        }

 

And I got the following error:

Error in call to API function "files/upload": Bad HTTP "Content-Type" header: "multipart/form-data; boundary=-----------------------------28947758029299".  Expecting one of "application/octet-stream", "text/plain; charset=dropbox-cors-hack".

 

Please suggest me.

Greg-DB
Dropbox Staff

This looks like essentially the same problem, but for a different endpoint. The code you're using must be causing RestSharp to change the Content-Type, perhaps when you're calling AddFile. You should send up the file content as an octet stream in the body, not as multipart form data.

Shilpa M.
New member | Level 1

I have try different approaches  to upload a new file but every time i got same error every time.

Error in call to API function "files/upload": Bad HTTP "Content-Type" header: "application/octet-stream;".  Expecting one of "application/octet-stream", "text/plain; charset=dropbox-cors-hack".

my code is

  string fullPathToUpload = metadataJson.path + "/" + externalFile.Name;
                string path = string.Format("{1}\"path\":\"{0}\"{2}", fullPathToUpload, "{", "}");
                request.AddHeader("Dropbox-API-Arg", path);
                request.AddHeader("Content-Type","application/octet-stream;");
                //  externalFile.ContentType = "application/octet-stream";
                //request.AddFile("file", externalFile.FileBody, externalFile.Name);//File body in byes Array
                request.AddObject(externalFile.FileBody, "data");                             request.AddHeader("Authorization", "Bearer " + accessToken);
            return client.Execute(request);

Please suggest.

 

Steve M.
Dropbox Staff

Read the error message carefully:

Error in call to API function "files/upload": Bad HTTP "Content-Type" header: "application/octet-stream;".  Expecting one of "application/octet-stream", "text/plain; charset=dropbox-cors-hack".

Steve M.
Dropbox Staff

BTW, even after you sort out the typo, I think your code is wrong. From my reading of the RestSharp documentation, you want something like this:

request.AddParameter("application/octet-stream", externalFile.FileBody, ParameterType.RequestBody);

I'm assuming that externalFile.FileBody is something like a byte array. If not, swap that out for whatever has the actual data.

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Shilpa M. New member | Level 1
  • User avatar
    Greg-DB Dropbox Staff
What do Dropbox user levels mean?