Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
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.
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
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
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>
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();
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.
Hi there!
If you need more help you can view your support options (expected response time for a ticket is 24 hours), or contact us on X or Facebook.
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!