Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
Hi. I'm developing a windows desktop app in order to get the dropbox links of the files inside my dropbox directory. I want to use the 'create_shared_link_with_settings' Dropbox api but when I execute POST method I only get vb exception: 'HTTP Error 400 Bad request'.
I'm a basic Dropbox user and developing app for personal use (I don't need to enable additional users). I registered my app and got OAuth2 token.
The data I'm sending is:
{"path":"/Public/MyFile.pdf","settings":{"requested_visibility":"public"}}
In the headers:
"Authorization: Bearer myOAuth2TokenHere"
And the url: "https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings"
Here is the vb .net [FrameWork 4.0] code I'm using.
Dim sFormattedFilePath As String = ""
Dim sUri As String = ""
Dim sData As String = ""
Dim sJSON As String = ""
Dim pHeader As WebHeaderCollection = Nothing
Dim pRequest As HttpWebRequest = Nothing
Dim pResponse As HttpWebResponse = Nothing
Try
sFormattedFilePath = "/Public/MyFile.pdf"
sData = "{" & Chr(34) & "path" & Chr(34) & ":" & Chr(34) & sFormattedFilePath & Chr(34) & "," & Chr(34) & "settings" & Chr(34) & ":{" & Chr(34) & "requested_visibility" & Chr(34) & ":" & Chr(34) & "public" & Chr(34) & "}}"
pHeader = New WebHeaderCollection()
pHeader.Clear()
pHeader.Add("Authorization: Bearer myOAuth2TokenHere")
sUri = "https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings"
pRequest = DirectCast(HttpWebRequest.Create(sUri), HttpWebRequest)
pRequest.ContentType = "application/json"
pRequest.ContentLength = sData.Length
pRequest.Headers = pHeader
pRequest.Method = "POST"
Using s As Stream = pRequest.GetRequestStream()
s.Write(System.Text.ASCIIEncoding.Default.GetBytes(sData), 0, sData.Length)
End Using
pResponse = DirectCast(pRequest.GetResponse(), HttpWebResponse)
Using sr As New StreamReader(pResponse.GetResponseStream())
sJSON = sr.ReadToEnd()
End Using
Catch ex As Exception
MsgBox(ex.Message)
End Try
If you are wondering:
The content of var sData is
{"path":"/Public/MyFile.pdf","settings":{"requested_visibility":"public"}}
The exception occurs when executing line:
pResponse = DirectCast(pRequest.GetResponse(), HttpWebResponse)
And the message of exception is:
HTTP Error 400 Bad request
I used exactly the same vb.net code when calling 'get_shared_links' api and it worked fine, that's why I was sure the code was correct but when I call 'create_shared_link_with_settings' the code doesn't work.
Any clue of what's happening?? please Help :'(
What's the body of the response? It should contain more useful error information.
My code saves 'the body of the response' into the var: sJSON however, my code crashes before it gets to the line which reads that response. In this case, the one who is throwing the exception is the vb.net compiler.
I don't know if it could be useful but analyzing the information included into the vb.net exception I found this:
Headers = {Transfer-Encoding: chunked Connection: keep-alive X-Dropbox-Request-Id: baad501b308ddd5c1310f7af856563fe Content-Type: text/plain; charset=utf-8 Date: Wed, 16 Mar 2016 19:02:33 GMT Server: nginx }
StatusCode = BadRequest {400}
StatusDescription = "Bad Request"
Ok, I fixed my code to be able to read 'the body of the response' by performing a cast from the vb exception class. What I've got is this:
"Error in call to API function "sharing/create_shared_link_with_settings": This function requires its argument in the HTTP request body, but your request body is empty."
That error indicates that your request body was empty, instead of containing the parameters. You'll need to debug the code that makes the request to check why your data isn't getting sent up in the body.
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!