Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
Hi, I am using the v2 API for http. What I found is that if a file or folder name contains a special character, like a German Umlaut like ä, I get a 400 error on upload and download. Is this a bug?
During download the response text is "Error in call to API function "files/download": HTTP header "Dropbox-API-Arg": could not decode input as JSON".
During upload the response text is "Error in call to API function "files/upload": HTTP header "Dropbox-API-Arg": could not decode input as JSON".
Seems like there's a bug in the JSON.parse() function on DropBox's side. The same JSON stringified text with an umlaut in the file name will parse just fine in the browsers I tested.
For these calls with the parameters in the header, you need to escape these characters. That is, when you use the “Dropbox-API-Arg” header, you need to make it “HTTP header safe”. This means using JSON-style “\uXXXX” escape codes for the character 0x7F and all non-ASCII characters.
Some, but not all, languages/libraries do this for you. For example, for JavaScript, to do this yourself you could do something like this:
var charsToEncode = /[\u007f-\uffff]/g;
function http_header_safe_json(v) {
return JSON.stringify(v).replace(charsToEncode,
function(c) {
return '\\u'+('000'+c.charCodeAt(0).toString(16)).slice(-4);
}
);
}
and then:
"Dropbox-API-Arg": http_header_safe_json(arg)
Thanks Greg, that worked. And thanks for answering on a sunday too.
I got a solution from Dropbox support that works. You need to escape all non-ASCII characters 'JSON' style. Which means all characters with character codes > 0x7F have to be replaced by a 4-digit hex code (it's hex unicode), preceeded by '\u'. For example Ä would be replaced with \u00c4 in the file name string.
Replacing parts of a string is very straightforward in Javascript, but a little more involved in a language like C.
For example, in C#, using Json.NET/JsonTextWriter, that would look like:
var sb = new StringBuilder(); var textWriter = new JsonTextWriter(new StringWriter(sb)); textWriter.StringEscapeHandling = StringEscapeHandling.EscapeNonAscii; // Write things to text writer textWriter.WriteStartArray(); textWriter.WriteValue("Hello"); ... var result = sb.toString().Replace("\x7f", "\\u007f");
Or, using Json.NET/JsonConvert:
var serializerSettings = new JsonSerializerSettings(); serializerSettings.StringEscapeHandling = StringEscapeHandling.EscapeNonAscii; var result = JsonConvert.SerializeObject(..., serializerSettings); result = result.Replace("\x7f", "\\u007f");
My file path is saved in a c++ std::string. Can you please provide code to do that character skipping in C++? Thanks.
@mi2 I unfortunately don't have a sample for C++. Please refer to my C# samples above and translate them for your platform.
Is there any way to do this in java?
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!