Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
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
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
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.
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.
Specifically, when using RestClient with API v2 calls, it looks like you should use .AddJsonBody instead of .AddParameter.
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.
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.
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.
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".
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.
Hi there!
If you need more help you can view your support options (expected response time for a ticket is 24 hours), or contact us on X or Facebook.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!