-1

I want sending POST requests to service under spring security. if i using form submission (with hidden input) - it's working fine. But i don't know, how send request with $http.post. my form:

<form method="POST" type="submit" action="http://localhost:8888/rest/post1" />
    <button>345</button>
    <input name="${_csrf.parameterName}"
           value="${_csrf.token}" />
</form>

it work.

I save params:

<script>
        var a = "${_csrf.parameterName}";
        var b = "${_csrf.token}";
</script>

My ng-click function:

$http.post('http://localhost:8888/rest/post1', {"_csrf": b}, {
    "headers" :
    { "_csrf" : b }
}).success(function(data) {
    console.log("ok");

}).error(function(){

    console.log("no");
});

It always writing "no".

I think, that need to send variable as webForm, but how do it in $http.post?

Fiddler write: Content-Type is 'application/json'; this inspector supports 'x-www-form-urlencoded' only

Server console:

org.springframework.web.servlet.PageNotFound handleHttpRequestMethodNotSupported
WARNING: Request method 'POST' not supported

Help me, please!

DanStopka
  • 535
  • 5
  • 22

2 Answers2

1

You might need to set "_csrf" in the request header. You are passing it as an argument for a method. This won't work. I am not much aware of angular.js but you must need to pass it as request header or request payload. I found some reference. Here it goes:

$httpProvider.defaults.xsrfCookieName = 'csrftoken';
$httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken';

Hope this helps you to solve your problem. Cheers !!!

Vinod Chandak
  • 373
  • 1
  • 6
  • 15
0

I solved this)

var request = $http({
    method: "post",
    url: "http://localhost:8888/rest/post1",
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    transformRequest: function(){return a+"="+b;},
    data: {}
});
DanStopka
  • 535
  • 5
  • 22