0

I want to add a header into Restangular X-CSRFToken. How can I do this?

This is what I have tried:

.controller("LoginFormController", ['$scope', 'Restangular', "NoBaseURL",
        function ($scope, Restangular, NoBaseURL) {
            // Submit form
            $scope.submit = function () {

                var token = $('input[name=csrfmiddlewaretoken]').val();


                post_data = {
                    username: $scope.username, password: $scope.password,
                    Restangular.headers: {'X-CSRFToken':token},
                }

                var login_post = NoBaseURL.all('/login/').post(post_data)

                event.preventDefault();
            }

        }])
2
  • do you want to add it to every request as a default parameter or a specific one? Commented Jul 14, 2014 at 10:05
  • @wickY26 only to this request Commented Jul 14, 2014 at 10:07

1 Answer 1

1

default post method of restangular

post(subElement, elementToPost, [queryParams, headers]) 

(from document) Does a POST and creates a subElement. Subelement is mandatory and is the nested resource. Element to post is the object to post to the server

EXAMPLE

account.customPOST({name: "My Message"}, "", {}, {headerKey : "headerValue"})

so if we turn it to your solution it would be something like this

NoBaseURL.all('/login/').post(post_data, "", {}, {'X-CSRFToken':token})

hope it helps...

Sign up to request clarification or add additional context in comments.

1 Comment

I can get it to work this way around: post_data, "", {"X-CSRFToken": token}, {}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.