cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox here.

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: 

HTTP API Path contain Chinese response 409 path not found?

HTTP API Path contain Chinese response 409 path not found?

jianwen
Explorer | Level 3

I reply my question here:https://www.dropboxforum.com/t5/API-support/International-characters-in-filenames/m-p/175750#M6947

but not resolved,so i open a new post to illustate my issue:

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 send request to dropbox api,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/.

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.By the way,if there is no Chinese in path,response is success.

 

a4900c43ly1fj5n2pr0u3j20zv0emwgj

6 Replies 6

Greg-DB
Dropbox Staff
If you're sending the same value as the SDK, please double check that you're connected to the same account/app. It's possible you're using an access token for a different account/app that in fact doesn't have anything at that path.

Otherwise, please share the full code we can use to reproduce the issue.

jianwen
Explorer | Level 3

Account/app is correct, In the same folder for my test,i create two folders one path contains Chinese,one just pure English letters,the later folder test success,so the Account/APP is not the problem.

Here is the code https://gist.github.com/user20161119/84681489c04ef912d3646ab6ef05c357

Actually,just try to use httpclient to send the http request to dropbox api,will get the same result as mine.thanks

jianwen
Explorer | Level 3

Here is my code snippet,https://gist.github.com/user20161119/84681489c04ef912d3646ab6ef05c357

Actually,just try to use java httpclient to send to https://www.dropbox.com/developers/documentation/http/documentation#files-get_preview endpoint,will reproduce my issue,thanks

Greg-DB
Dropbox Staff

Your code sample uses some custom classes so I can't run it, but this runs fine for me:

 

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class Main {

    public static void main(String[] args) throws IOException {

        String ACCESS_TOKEN = "<ACCESS_TOKEN>";
        String filePath = "/blackdog/中文prd.docx";

        JsonFactory jsonFactory = new JsonFactory();
        OutputStream outputStream = new ByteArrayOutputStream();
        JsonGenerator jsonGenerator = jsonFactory.createGenerator(outputStream);
        jsonGenerator.setHighestNonEscapedChar(0x7E);
        jsonGenerator.writeStartObject();
        jsonGenerator.writeStringField("path", filePath);
        jsonGenerator.writeEndObject();
        jsonGenerator.close();

        String arg = outputStream.toString();
        System.out.println(arg);

        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            HttpPost httpost = new HttpPost("https://content.dropboxapi.com/2/files/get_preview");
            httpost.setHeader("Authorization", "Bearer " + ACCESS_TOKEN);
            httpost.setHeader("Dropbox-API-Arg", arg);

            ResponseHandler<String> responseHandler = new ResponseHandler<String>() {

                @Override
                public String handleResponse(final HttpResponse response) throws IOException {
                    int status = response.getStatusLine().getStatusCode();
                    System.out.println(status);
                    HttpEntity entity = response.getEntity();
                    return entity != null ? EntityUtils.toString(entity) : null;
                }
            };

            String responseBody = httpclient.execute(httpost, responseHandler);
            System.out.println(responseBody);

        } finally {
            httpclient.close();
        }

    }
}

That outputs:

{"path":"/blackdog/\u4E2D\u6587prd.docx"}
200
%PDF-1.4
%Ç쏢
<more PDF content truncated for brevity>

jianwen
Explorer | Level 3

thanks,

from your code, the difference is the following snippet i use Jackson ObjectMapper in my app,i will try your code later.

JsonFactory jsonFactory = new JsonFactory();
        OutputStream outputStream = new ByteArrayOutputStream();
        JsonGenerator jsonGenerator = jsonFactory.createGenerator(outputStream);
        jsonGenerator.setHighestNonEscapedChar(0x7E);
        jsonGenerator.writeStartObject();
        jsonGenerator.writeStringField("path", filePath);
        jsonGenerator.writeEndObject();
        jsonGenerator.close();

jianwen
Explorer | Level 3

I tried use the following code to generate the Dropbox-API-Arg header value,and work correctly

 DropboxContentReq req = new DropboxContentReq(DropboxAPI.PREVIEW);
req.setHttp_method(HttpMethod.POST);
String filePath = "/blackdog/中文prd.docx";

JsonFactory jsonFactory = new JsonFactory();
OutputStream outputStream = new ByteArrayOutputStream();
JsonGenerator jsonGenerator = jsonFactory.createGenerator(outputStream);
jsonGenerator.setHighestNonEscapedChar(0x7E);
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField("path", filePath);
jsonGenerator.writeEndObject();
jsonGenerator.close();

String arg = outputStream.toString();
System.out.println(arg);


DropboxPathReq _req = new DropboxPathReq();
//convert Chinese to Unicode /blackdog/\\u4E2D\\u6587prd.docx
filePath = StringEscapeUtils.escapeJava(filePath);
_req.setPath(filePath);
//concat the json work
String json_header = "{\"path\":\"" + filePath + "\"}";

//use arg as header value work correct
//req.getHeaders().add("Dropbox-API-Arg",json_header);

req.getHeaders().add("Dropbox-API-Arg",json_header);

String json = dropbox_api_service.callDropbox(req);
LoggerUtils.logInfo(this.getClass(), json);

 and i concat the Dropbox-API-Arg header json value,work correctly too,

 String json_header = "{\"path\":\"" + filePath + "\"}";

 I use Jackson ObjectMapper generate the header value before,the difference is 

{"path":"/blackdog/\\u4E2D\\u6587prd.docx"}

 there are two \ in the json string,maybe is violate the HTTP HEADER spec,i will check http header value spec later,any way thanks for your time.

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    jianwen Explorer | Level 3
  • User avatar
    Greg-DB Dropbox Staff
What do Dropbox user levels mean?