Forum Discussion

Artem K.'s avatar
Artem K.
New member | Level 1
10 years ago

International characters in filenames

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

  • Artem K.'s avatar
    Artem K.
    New member | Level 1
    10 years ago

    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.

  • Artem K.'s avatar
    Artem K.
    New member | Level 1
    10 years ago

    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.

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    10 years ago

    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 })
  • Artem K.'s avatar
    Artem K.
    New member | Level 1
    10 years ago

    Thank you Gregory,

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

  • jianwen's avatar
    jianwen
    Explorer | Level 3
    9 years ago

    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?

  • jianwen's avatar
    jianwen
    Explorer | Level 3
    9 years ago

    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]"
    

     

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    9 years ago

    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);
  • jianwen's avatar
    jianwen
    Explorer | Level 3
    9 years ago

    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.

  • jianwen's avatar
    jianwen
    Explorer | Level 3
    9 years ago

    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.

About Dropbox API Support & Feedback

Node avatar for 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!