Make a js request with 2 methods POST and PUT

40 Views Asked by At

I got api for creating cats with authorisation token

curl -X POST -H "Authorization: JWT <dat_token>" -X PUT -H "Content-Type: application/json" -d '{"name":"SuperApi2","breed":"Bite"}' http://127.0.0.1:8000/cats/api/

how to write request in AngularJs for this operation? It has 2 methods POST and PUT

I tried to play with something like this but it does not work

var req = {
    method: 'POST',
    url:'http://127.0.0.1:8000/cats/api/',
    headers: {
        'Authorization':'<data_token>',
        'Content-Type': 'application/json'
    },
    data: {"name":"AngularJs","breed":"Bite"}
};
$http(req).then(
    function(qwe) { console.log(qwe) }, 
    function(error) { alert(error.toSource()) }
);
1

There are 1 best solutions below

0
On

Ok, this operation does not really need POST method, only put

var req = {
    method: 'PUT',
    url:'http://127.0.0.1:8000/cats/api/',
    headers: {
        'Authorization':'<data_token>',
        'Content-Type': 'application/json'
    },
    data: {"name":"AngularJs","breed":"Bite"}
};
$http(req).then(
    function(qwe) { console.log(qwe) }, 
    function(error) { alert(error.toSource()) }
);