We’re Still Here to Help (Even Over the Holidays!) - find out more here.
Forum Discussion
Klaus A.
9 years agoHelpful | Level 5
Umlauts in file names
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.
11 Replies
- Greg-DB9 years ago
Dropbox Community Moderator
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)
- Klaus A.9 years agoHelpful | Level 5
Thanks Greg, that worked. And thanks for answering on a sunday too.
- developer_net9 years agoExplorer | Level 4Hi Klaus
I have the same problem with a .net application.
I tried differend solutions to change the "Ä" in my path, but it will not work.
Do you have a tip for a dropbox newcommer? How have you solved the exception
My (notworking) path with umlaut looks like this:
{"path": "/Bild mit Umlaut/Ä Wasser.jpg","format": "jpeg","size": "w128h128"}
Best regards Horst - Klaus A.9 years agoHelpful | Level 5
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.
- Greg-DB9 years ago
Dropbox Community Moderator
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"); - developer_net9 years agoExplorer | Level 4Thank you Klaus!
Upgrading vom Json.net 4.5 to 10 and using the StringEscapeHandling.EscapeNonAscii solves my problem ;-)
Best regards, Horst - mi29 years agoNew member | Level 2
My file path is saved in a c++ std::string. Can you please provide code to do that character skipping in C++? Thanks.
- Greg-DB9 years ago
Dropbox Community Moderator
mi2 I unfortunately don't have a sample for C++. Please refer to my C# samples above and translate them for your platform.
- Abhishek38810008 years agoNew member | Level 2
Is there any way to do this in java?
- Abhishek38810008 years agoNew member | Level 2
Is there any way to do the same in java.
My folder name "Å,å,Æ,æ,Ø,ø" would be replaced with what string?
I would appreciate any reply.
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.
If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.
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!