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

CSRF Failed when POST data using Alamofire #1118

Closed
Windsooon opened this Issue Mar 14, 2016 · 1 comment

Comments

Projects
None yet
2 participants
@Windsooon

Windsooon commented Mar 14, 2016

I using django-reat-framework as backend and using SessionAuthentication and TokenAuthentication. This work well when I use httpie send request

http POST http://127.0.0.1:8000/api/users/ email="abc@abc.com" user_name="abc" passwod="1234"

but when I use Alamofire

Alamofire.request(.POST, "http://127.0.0.1:8000/api/users/", parameters: ["email": emailField.text!, "user_name": usernameField.text!, "password": passwordField.text!], encoding: .URL )
            .responseJSON { response in
                print(response.request)  // original URL request
                print(response.response) // URL response
                print(response.data)     // server data
                print(response.result)
                if let JSON = response.result.value {
                    print("JSON: \(JSON)")
                }
        }

return this

Optional(<NSMutableURLRequest: 0x7fe24e15d640> { URL: http://127.0.0.1:8000/api/users/ })
Optional(<NSHTTPURLResponse: 0x7fe24bf3a080> { URL: http://127.0.0.1:8000/api/users/ } { status code: 403, headers {
    Allow = "GET, POST, HEAD, OPTIONS";
    "Content-Type" = "application/json";
    Date = "Fri, 11 Mar 2016 13:09:59 GMT";
    Server = "WSGIServer/0.2 CPython/3.4.3";
    Vary = "Accept, Cookie";
    "X-Frame-Options" = SAMEORIGIN;
} })
Optional(<7b226465 7461696c 223a2243 53524620 4661696c 65643a20 43535246 20746f6b 656e206d 69737369 6e67206f 7220696e 636f7272 6563742e 227d>)
SUCCESS
JSON: {
    detail = "CSRF Failed: CSRF token missing or incorrect.";
}

But 127.0.0.1:8000/api/users/ don't need any permission, and I didn't send csrf token when I using httpie. So, What's wrong here?

@cnoon

This comment has been minimized.

Member

cnoon commented Mar 19, 2016

Hi @Windsooon,

You probably need to use the parameter encoding .URLEncodedInURL instead of .URL. Your POST is putting the payload in the HTTPBody and not the query parameters. Additionally, you can use the following in the future to help you debug this type of problem.

let request = Alamofire.request(.POST, URLString, parameters:parameters, encoding: .URL)
debugPrint(request)

This will print out the cURL command making it MUCH easier to see what's actually going on.

In the future, I'd suggest opening a question on Stack Overflow instead. We use the GitHub project for bug reports and feature requests.

Best of luck!


From our Contribution Guidelines

Asking Questions

We don't use GitHub as a support forum. For any usage questions that are not specific to the project itself, please ask on Stack Overflow instead. By doing so, you'll be more likely to quickly solve your problem, and you'll allow anyone else with the same question to find the answer. This also allows maintainers to focus on improving the project for others.

@cnoon cnoon closed this Mar 19, 2016

@cnoon cnoon added the support label Mar 19, 2016

@cnoon cnoon self-assigned this Mar 19, 2016

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