Forum Discussion

Artem K.'s avatar
Artem K.
New member | Level 1
9 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

  • Artem K.'s avatar
    Artem K.
    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.

  • Artem K.'s avatar
    Artem K.
    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.

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox Staff

    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 })
    • jianwen's avatar
      jianwen
      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?

      • Greg-DB's avatar
        Greg-DB
        Icon for Dropbox Staff rankDropbox Staff

        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

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

       

  • Artem K.'s avatar
    Artem K.
    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.