cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Check out how our Community members are using Dropbox here!
Close

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: 

International characters in filenames

Highlighted

International characters in filenames

New member | Level 1
New member | Level 1

Hi,

I am migrating to V2 of REST API and I am having problems with endpoints that require passing arguments using "Dropbox-API-Arg" header (for example, https://content.dropboxapi.com/2/files/download).

The problem is I am not being able to pass filenames containing international characters.

For example, I have a file "моя запись.txt" saved in my folder, with path "/моя запись.txt"

When I try to simply pass this string in the header, I get an error from ajax:

SyntaxError: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': '{"path":"/моя запись.txt"}' is not a valid HTTP header field value.

In V1 of API I simply encoded every path using encodeURIComponent function. In that case the path "/моя запись.txt" would become "%2F%D0%BC%D0%BE%D1%8F%20%D0%B7%D0%B0%D0%BF%D0%B8%D1%81%D1%8C.txt" and that worked for V1.

However, when I try the same with the V2, I get the following error from API:

Error in call to API function "files/download": HTTP header "Dropbox-API-Arg": path: '%2F%D0%BC%D0%BE%D1%8F%20%D0%B7%D0%B0%D0%BF%D0%B8%D1%81%D1%8C.txt' did not match pattern...

I tried to bypass this validation by keeping the slash and encoding only the file name itself. In this case the filepath becomes "/%D0%BC%D0%BE%D1%8F%20%D0%B7%D0%B0%D0%BF%D0%B8%D1%81%D1%8C.txt". This seems to be recognized as a valid path, but I get a new error:

"path/not_found/"

How do I correctly pass that path "/моя запись.txt" to the API endpoint?

Thank you,

Artem

10 Replies 10
Highlighted

Re: International characters in filenames

New member | Level 1
New member | Level 1

In the end I was able to pass such a file name using URL parameter arg, encoded using encodeURIComponent function. So it seems that "Dropbox-API-Arg" header is not really usable in multi-language scenarios.

Highlighted

Re: International characters in filenames

New member | Level 1
New member | Level 1

API explorer seems to encode the same file path as "/\u043c\u043e\u044f \u0437\u0430\u043f\u0438\u0441\u044c.txt". OK, I just probably need to find the way to do this conversion in JavaScript.

Highlighted

Re: International characters in filenames

Dropboxer
Dropboxer

Hi Artem, as you saw in the API Explorer, 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 something like:

'Dropbox-API-Arg': http_header_safe_json({ path: dropboxFilePath })
Highlighted

Re: International characters in filenames

New member | Level 1
New member | Level 1

Thank you Gregory,

Ideally I would prefer to use some standard function instead of implementing encoding myself, but your example looks simple/robust enough.

Highlighted

Re: International characters in filenames

Explorer | Level 3

There is Chinese in path like:"/blackdog/中文prd.docx“,i see the api explore send Dropbox-API-Arg as follow:

Dropbox-API-Arg:{"path":"/blackdog/\u4e2d\u6587prd.docx"}

I am using java to implement http,my Dropbox-API-Arg as follow:

 

"Dropbox-API-Arg:  {"path":"/blackdog/\\u4E2D\\u6587prd.docx"}"

In java \\ standard for one \,and dropbox response as follow:

 

path/not_found/.

How could i resolve the issue?

Highlighted

Re: International characters in filenames

Explorer | Level 3

I try the url arg parameter as follow(url encoded):

"POST /2/files/get_preview?arg=%257B%2522path%2522%253A%2522%252Fblackdog%252F%25E4%25B8%25AD%25E6%2596%2587PRD.docx%2522%257D HTTP/1.1[\r][\n]"

not work neither,get error as follow

 

 "Error in call to API function "files/get_preview": URL parameter "arg": could not decode input as JSON[\r][\n]"

 

Highlighted

Re: International characters in filenames

Dropboxer
Dropboxer

This may depend on your setup, but are you sure you need the double '/'? Have you tried it without it? i.e.,

 

"Dropbox-API-Arg: {"path":"/blackdog/\u4e2d\u6587prd.docx"}"

 

In any case, we recommend either using the official Dropbox Java SDK, or having a library do the encoding for you. E.g., for Java, using Jackson, it would look like:

 

JsonFactory f = ...
JsonGenerator g = f.createGenerator(...);
g.setHighestNonEscapedChar(0x7E);
Highlighted

Re: International characters in filenames

Explorer | Level 3

Actually i use Jackson format json as follow:

        String path = "/blackdog/中文PRD.docx";
        path = StringEscapeUtils.escapeJava(path);
        path_req.setPath(path);
        String header_json = JsonUtils.writeValue(path_req);
        LoggerUtils.logInfo(this.getClass(), header_json);

In java double / standards /,so following code 

"Dropbox-API-Arg: {"path":"/blackdog/\u4e2d\u6587prd.docx"}"

is correct in Java.

Highlighted

Re: International characters in filenames

Explorer | Level 3

I debuged Dropbox java sdk,and the header Dropbox-API-Arg value is the same as mine

 "Dropbox-API-Arg:  {"path":"/blackdog/\\u4E2D\\u6587prd.docx"}[\r][\n]"

but i got 409 path/not_found/..,dropbox sdk get right response,i don not why.

a4900c43ly1fj5n2pr0u3j20zv0emwgj

Work Smarter with Dropbox

The way we work is changing. Share and discover new ways to work smarter with Dropbox in our community.

Sound good? Let's get started.
Who's talking

Top contributors to this post

What do Dropbox user levels mean?
Need more support?