Hintham
Hintham

Reputation: 1086

Flurl PostUrlEncoded does GET instead of POST

I must be missing something very obvious, but I can't tell what. I have a DoLoginAsync like so:

private async Task DoLoginAsync(bool force = false)
        {
            try
            {
                if (client.Cookies.ContainsKey("user_credentials") && !force)
                {
                    return;
                }
                var html = client.Request("login").GetStringAsync().Result;
                var doc = new HtmlDocument();
                doc.LoadHtml(html);
                var csrf_token = doc.DocumentNode.SelectNodes("//meta[@name='csrf-token']").First().GetAttributeValue("content", string.Empty);
                var values = new Dictionary<string, string>
                {
                    { "user_session[email]", user },
                    { "user_session[password]", password },
                    { "authenticity_token", csrf_token }
                };
                var result = await client.Request("user_session").PostUrlEncodedAsync(values);
            }
            catch (Exception e)
            {
            }

When I run this code in a test with a breakpoint in the catch clause I get an exception

Call failed with status code 404 (Not Found): GET http://www.whatever.com/user_session

WTF? I'm expecting PostUrlEncodedAsync to do a POST, not a GET. Anybody have an idea why this can happen?

The Flurl client is instantiated as client = new FlurlClient(BASE_URL).EnableCookies();

UPDATE

Tried the following test which fails with the same exception

[TestMethod]
        public async Task TheTest()
        {
            var message = "";
            try
            {
                var client = new FlurlClient("http://www.slimmemeterportal.nl/").EnableCookies();
                var html = await client.Request("login").GetStringAsync();
                var doc = new HtmlDocument();
                doc.LoadHtml(html);
                var csrf_token = doc.DocumentNode.SelectNodes("//meta[@name='csrf-token']").First().GetAttributeValue("content", string.Empty);
                var values = new Dictionary<string, string>
                {
                    { "user_session[email]", "******" },
                    { "user_session[password]", "******" },
                    { "commit", "inloggen" }, // Not sure if this is actually needed, but it is in the website's request parameters.
                    { "authenticity_token", csrf_token }
                };
                var result = await client.Request("user_session").PostUrlEncodedAsync(values);
            }
            catch (FlurlHttpException ex)
            {
                message = ex.Message;
            }
            Assert.AreEqual("Call failed with status code 404 (Not Found): POST http://www.slimmemeterportal.nl/user_session", message);
        }

Upvotes: 2

Views: 1184

Answers (1)

Hintham
Hintham

Reputation: 1086

Mystery solved: As it turns out after some debugging with Wireshark, the website was returning HTTP status code 301. As explained here the default action is to follow the URI in the response's location header using a GET even if the original request was a POST.

Upvotes: 5

Related Questions