We’re Still Here to Help (Even Over the Holidays!) - find out more here.
Forum Discussion
jianwen
9 years agoExplorer | Level 3
HTTP API Path contain Chinese response 409 path not found?
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 C...
Greg-DB
Dropbox Community Moderator
9 years agoYour 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
9 years agoExplorer | 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.
About 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!