Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP 400 Bad Request On Download #77

Closed
pchretien opened this issue Feb 26, 2018 · 15 comments
Closed

HTTP 400 Bad Request On Download #77

pchretien opened this issue Feb 26, 2018 · 15 comments

Comments

@pchretien
Copy link

The server now returns an HTTP 400 Bad Request on a files/download call to the API because of missing Content-Type.

In DropboxRequestHandler.cs, line 443, the Content type for RouteStyle.Download is set to null while the server needs at least a text/plain.

I simply removed the line setting the Content-Type null and it worked with the default Content-Type value. I guess the long term solution should be more in line with the API requirements.

`case RouteStyle.Download:
request.Headers.Add(DropboxApiArgHeader, requestArg);

                // This is required to force libcurl remove default content type header.
                request.Content = new StringContent("");
                //request.Content.Headers.ContentType = null;

                completionOption = HttpCompletionOption.ResponseHeadersRead;
                break;

`

@greg-db
Copy link
Contributor

greg-db commented Feb 26, 2018

Can you share the code you're using and the full output of the 400 error you're seeing? The DownloadAsync method is still working for me without any modifications. Thanks in advance!

@pchretien
Copy link
Author

pchretien commented Feb 27, 2018

I was using the lib as a Nuget in a Xamarin Forms project. The library was working just fine on both Android and iPhone but then, something changed at Dropbox and we started having the following error [image] on Android. The lib continues to work just fine on iPhone.

I then switched to the lib compiled on my machine and fixed the bug with the method I described before. I will post the full stack trace when I'm back at the office ...

screenshot_2018-02-26-11-10-35

@greg-db
Copy link
Contributor

greg-db commented Feb 27, 2018

Thanks! That error says that the client is actually sending a "Content-Type" of "application/x-www-form-urlencoded". That was never valid for API v2, and the SDK doesn't set it, so it appears that something else on the system is overriding and setting it.

In any case, the .NET SDK isn't officially supported for Xamarin, but I'll pass this along as a feature request.

@tipa
Copy link

tipa commented Apr 30, 2019

I would very much appreciate this being fixed. Wouldn't commenting out this line be an easy fix?

request.Content.Headers.ContentType = null;

@greg-db
Copy link
Contributor

greg-db commented Apr 30, 2019

@tipa Thanks for the note. The team hasn't worked on implementing and testing official support for Xamarin in this SDK, but I'll add your vote to the feature request.

@tipa
Copy link

tipa commented Apr 30, 2019

Thanks!
In addition, I think the problem originates from Xamarin using the "AndroidClientHandler" as HttpMessageHandler. So using the current lib version, a workaround would be explicitly setting the (less efficient) HttpClientHandler
dbx = new DropboxClient(accessToken, new DropboxClientConfig() { HttpClient = new HttpClient(new HttpClientHandler()) });

@cmiles
Copy link

cmiles commented May 5, 2019

@greg-db Fwiw thought I would leave a note here that I ran into this issue today with a Xamarin.Forms Android Application - thanks to the last comment from @tipa, using HttpClientHandler seems to have solved the problem for me...

@allanchao
Copy link

I had the same issue on Xamarin.Android native, HttpClientHandler worked for me too. Thank you @tipa!

@Orgbrat
Copy link

Orgbrat commented Jun 1, 2019

I also had this error or issue on Xamarin Forms Android application (UWP and iOS worked fine) , the HttpClientHandler workaround worked for me too. Thank you @tipa!

@sgarrepallygithub
Copy link

tipa, thank you very much. i spent 1 week to resolve this problem. i had code in windows desktop app. but same code could not work in Xamarin.

@nathankim0
Copy link

Thank you @tipa !! It helps me.

@tipa
Copy link

tipa commented Jun 12, 2022

It appears that my previously posted workaround stopped working after migrating my project to a Android .NET6.

I now was able to implement an alternative workaround:

public class MyAndroidMessageHandler : AndroidMessageHandler
{
    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        if (request.RequestUri.AbsolutePath.Contains("files/download"))
        {
            request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
        }
        return base.SendAsync(request, cancellationToken);
    }
}

dbx = new DropboxClient(accessToken, new DropboxClientConfig() { HttpClient = new HttpClient(new MyAndroidMessageHandler()) });

@greg-db would it be possible to fix this in the SDK as suggested here?
I can also provide a demo project if that helps you reproducing the problem

@greg-db
Copy link
Contributor

greg-db commented Jun 13, 2022

@tipa Thanks for the note! We have filed this as a request with the team to officially support this SDK in Xamarin, such as potentially include that fix, but I don't have any news on that.

@tipa
Copy link

tipa commented Aug 22, 2022

Has there been any feedback from the team on this?
The workarounds that I currently have to use (e.g. for this issue & #262) make me consider writing the API access logic myself instead of using the SDK.
That would also reduce my app size significantly (largely due to the Newtonsoft.Json dependency, see #276).
It looks like work on this SDK has been stopped apart from "Automated Spec Updates".

@greg-db
Copy link
Contributor

greg-db commented Aug 22, 2022

@tipa No, I don't have any news on this. Thanks for the feedback!

You can certainly integrate against the API directly without using the SDK if you prefer. You can find the documentation for the HTTPS endpoints here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants