You might see that the Dropbox Community team have been busy working on some major updates to the Community itself! So, here is some info on what’s changed, what’s staying the same and what you can expect from the Dropbox Community overall.
Forum Discussion
ingconti
3 years agoHelpful | Level 5
Re: Error in call to API function "files/upload": HTTP header "Dropbox-API-Arg":
good you are available
Said so, pls let me know How to fix it.
to recap:
a) my ap is written in swift, NO other components (neither official dropbox library, too big and too messy)
b) it did ...
Greg-DB
Dropbox Staff
I was referring to using something like JSONSerialization.data to build the JSON string from a dictionary for you, like in our example.
Regarding the remotePath value, while that may be a constant in your code (though you did not share where that is defined), is it possible you've changed what you assign to it in your code at some point?
I see in your latest comment that you included an example of the JSON string you're building, showing the path as "/OUT/aaaaa_000.jpg". Looking at that carefully, I see that the "_" character is not actually an ASCII underscore, but rather a Unicode character named "Fullwidth Low Line" (U+FF3F). That being the case, you will need to apply the encoding as documented, otherwise you'll run in to an error.
I just tried this myself and confirmed that running your code as shown with that character does cause a failure, but using the documented code to serialize and encode the parameters does work.
ingconti
3 years agoHelpful | Level 5
1) You wrote: "I was referring to using something like JSONSerialization.data to build the JSON string from a dictionary for you, like in our example.'
I Did write and teste din other environments and it worked, I have extensive Unit testing about.
2) d "Fullwidth Low Line" (U+FF3F).
3 consideration:
2a) it WORKED for an year, so what changed?
2b) THIS should be the 1st response
3b) why giving back such message should be a problem for backend?
I wil try Your encoding function, I will re-post.
- ingconti3 years agoHelpful | Level 5
I got from your example :
{"field":"some_\u00fc\u00f1\u00eec\u00f8d\u00e9_and_\u007f"}
if is correct, pls fix code that does NOT compile in swift any more, as JSONSerialization.data can throw:
let args = ["field": "some_üñîcødé_and_\u{7F}"]
if let jsonData = try? JSONSerialization.data(withJSONObject: args){
let encodedArgs = asciiEscape(utf8Decode(jsonData))
print(encodedArgs)
}
- Greg-DB3 years agoDropbox Staff
I can't say exactly what may have changed recently. The need to encode these values in HTTP headers has existed for a long time, so perhaps the path value itself was changed or the client network stack changed to expose this issue.
The issue was not immediately apparent based on your initial report, but I was sure to refer you to the relevant documentation and example once it was.
The value sent by the client needs to be properly encoded in order for the backend to be able to parse it.
And thanks for letting me know about the example! I'll ask the team to update that.
- ingconti3 years agoHelpful | Level 5
seems working !
THX
I used a reduced version for simple string, as path:
func DB_utf8Decode(_ data: Data) -> String {
return NSString(data: data, encoding: String.Encoding.utf8.rawValue)! as String
}
func DB_asciiEscape(_ s: String) -> String {
var out: String = ""
for char in s.unicodeScalars {
var esc = "\(char)"
if !char.isASCII {
esc = NSString(format:"\\u%04x", char.value) as String
} else {
if (char == "\u{7F}") {
esc = "\\u007f"
} else {
esc = "\(char)"
}
}
out += esc
}
return out
}
- ingconti3 years agoHelpful | Level 5
as a last side note:
you had to write:
- C# (2 cersions)
- java
- JavaScript
Objective-C
Python
PHP
Swift
So 8 versions when simply accepting binary JSON on server would have prevented all this stuff? and / or fixing on server directly?
seems, as minimum, suboptimal.
- Greg-DB3 years agoDropbox Staff
Thanks for following up. I'm glad to hear you got this working, and thanks for the feedback!
- ingconti3 years agoHelpful | Level 5
a more "swift" solution for string with a copiale of Unit test:
func DB_asciiEscape(_ s: String) -> String {
let out = s.unicodeScalars.reduce("", { (partialResult: String, char: UnicodeScalar) -> String in
if !char.isASCII {
return partialResult + String(format:"\\u%04x", char.value)
} else {
if (char == "\u{7F}") {
return partialResult + "\\u007f"
} else {
return partialResult + "\(char)"
}
}
}
)
return out
}
//------ UNIT TESTS:
func testUtf8Decode(){
let unescaped = "/OUT/aaaaa_000.jpg"
let escaped = DB_asciiEscape(unescaped)
print(escaped)
let contains = escaped.contains("uff3f000.jpg")
XCTAssert(contains, "not ecaped")
}
func testUtf8Decode2(){
let unescaped = "αβγ.jpg"
let escaped = DB_asciiEscape(unescaped)
print(escaped)
let contains = escaped.contains("\\u03b1\\u03b2\\u03b3")
XCTAssert(contains, "not ecaped")
}
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
5,910 PostsLatest Activity: 3 days agoIf 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 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!