json - Trying to connect to the Dropbox API v2 using C# Receive 400 error on all attempts -
the new dropbox api documentation at:
https://blogs.dropbox.com/developers/2015/04/a-preview-of-the-new-dropbox-api-v2/
i'm trying execute simple metadata call, having little success. here's code i'm using:
private void go() { var httpwebrequest = (httpwebrequest)webrequest.create("https://api.dropbox.com/2-beta/files/get_metadata"); httpwebrequest.contenttype = "text/json"; httpwebrequest.method = "post"; httpwebrequest.headers.add("authorization: bearer xxxxxxxxxxxxxxxxxxx"); using (var streamwriter = new streamwriter(httpwebrequest.getrequeststream())) { string json = "{\"path\": \"/avatar_501.png\"}"; streamwriter.write(json); streamwriter.flush(); streamwriter.close(); } var httpresponse = (httpwebresponse)httpwebrequest.getresponse(); using (var streamreader = new streamreader(httpresponse.getresponsestream())) { var result = streamreader.readtoend(); this.textbox1.text = result; } }
any massively appreciated!
if try code, you'll see body of 400 response, tells text/json
not valid content-type
. converted code console app, , i'm using newtonsoft.json json serialization. otherwise, difference between code , mine addition of exception handling body of 400.
class program { static void main(string[] args) { var httpwebrequest = (httpwebrequest)webrequest.create("https://api.dropbox.com/2-beta/files/get_metadata"); httpwebrequest.contenttype = "text/json"; httpwebrequest.method = "post"; httpwebrequest.headers.add("authorization: bearer <redacted>"); using (var streamwriter = new streamwriter(httpwebrequest.getrequeststream())) { streamwriter.write(jsonconvert.serializeobject(new { path = "/avatar_501.png" })); } httpwebresponse response; try { response = (httpwebresponse)httpwebrequest.getresponse(); } catch (webexception e) { response = (httpwebresponse)e.response; } console.writeline("status code: {0}", (int)response.statuscode); using (var streamreader = new streamreader(response.getresponsestream())) { console.writeline(streamreader.readtoend()); } console.readline(); } }
the output follows:
status code: 400 error in call api function "files/get_metadata": bad http "content-type" header: "text/json". expecting 1 of "application/json", "application/json; charset=utf-8", "text/plain; charset=dropbox-cors-hack".
changing content-type
application/json
causes call succeed.
Comments
Post a Comment