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: 

Re: Response headers

Response headers

wheel
Explorer | Level 4

when i use dropbox api for uploading file.  the http header contain chinese, for example:

"Dropbox-API-Arg:{\"path\": \"/Test/中文.doc\",\"mode\": \"add\",\"autorename\": true,\"mute\": false,\"strict_conflict\": false}".

it will response error message:

Error in call to API function "files/upload": HTTP header "Dropbox-API-Arg": could not decode input as JSON.

how should i do?

thanks.

11 Replies 11

Greg-DB
Dropbox Staff

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:

"Dropbox-API-Arg": http_header_safe_json(arg)

wheel
Explorer | Level 4

Thanks for replying.

I use dropbox api with winhttp and c.

Now, i'm trying to encode chinese to unicode. For example:

"工作.doc" -->  "\u5de5\u4f5c.doc" ,

and the header is :

Dropbox-API-Arg:{\"path\": \"/Test/\u5de5\u4f5c.doc\",\"mode\": \"add\",\"autorename\": true,\"mute\": false,\"strict_conflict\": false}.

But it also response error message:

Error in call to API function "files/upload": HTTP header "Dropbox-API-Arg": could not decode input as JSON.

So, if i want to create the filename with chinese,  what format should i input to header? 

Greg-DB
Dropbox Staff

It looks like you're properly escaping the Unicode characters, but you have an extra period at the end of the string, making it invalid JSON. Remove that extra "." and try again.

wheel
Explorer | Level 4

thanks for replying. 

the " . " it actualy not at the end of the string , i accidentally added it in the api-arg.

i tryed to solve it by other ways.

in addition , what formats for path in dropbox-api header? utf-8? or unicode ? 

Greg-DB
Dropbox Staff

The value sent in the header should be all ASCII, with JSON-style “\uXXXX” escape codes for the character 0x7F and all non-ASCII characters. The last sample you shared is correct, except for that period.

The path format itself is the same as elsewhere in the API. That is, non-root paths should start with "/" and also use "/" as the path component delimiter. 

If something still isn't working as expected, please share some code that reproduces the issue so we can investigate.

wheel
Explorer | Level 4

i add the header by the winapi WinHttpAddRequestHeaders.

and then,if the header as follows:

Dropbox-API-Arg:{\"path\": \"/Test/中文\",\"mode\": \"add\",\"autorename\": true,\"mute\": false,\"strict_conflict\": false}
Content-Type: application/octet-stream
Authorization: Bearer < token > 

my ide is vs2017, i put these string in the array . and i use the function MultiByteToWideChar() encode the header to  LPCWSTR. it will response error message.

in other words, if  the path contain Chinese characters, it will response errror message.

Greg-DB
Dropbox Staff

Can you share the actual complete code that reproduces this issue? It's hard to say what the problem might be from this description.

I do see that you're also escaping each " with a \. That may or may not be necessary, depending on the context in which you're doing this. I can't say without more information, but you may want to try removing those.

Also, in practice in recommend having a library build the JSON for you anyway, instead of manually constructing it.

Sekali
Explorer | Level 4

when I create a okhttp request in andorid , I create json from gson。I request https://content.dropboxapi.com/2/files/upload

and submit param json  like {"autorename":true,"mode":"add","mute":false,"path":"/\\u6d4b\\u8bd5.pdf","strict_conflict":false} . the Chinese char  is encode Unicode . But I got  a response below

upload result : {"error_summary": "path/malformed_path/", "error": {".tag": "path", "reason": {".tag": "malformed_path"}, "upload_session_id": "AAAAAAAAALgh7xt-gXMBJQ"}}

and I don't know what reason , please help me.

 

 

Sekali
Explorer | Level 4

here is request code:

OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("application/octet-stream");
RequestBody body = RequestBody.create(mediaType, uploadfile);
Request request = new Request.Builder()
.url("https://content.dropboxapi.com/2/files/upload")
.method("POST", body)
.addHeader("Authorization", "Bearer "+token)
//"{\"path\": \"/w.pdf\",\"mode\": \"add\",\"autorename\": true,\"mute\": false,\"strict_conflict\": false}"
.addHeader("Dropbox-API-Arg", requestJson)
.addHeader("Content-Type", "application/octet-stream")
.build();

request json

{"autorename":true,"mode":"add","mute":false,"path":"/\\u6d4b\\u8bd5.pdf","strict_conflict":false} 

 

when I request to upload file which named only contains English char ,it success.  when it contains Chinese char ,it failed

Need more support?