Search code examples
angularjsrestangular-resource

Is it possible to set content-type in angular $resource without body


I'm consuming an API that accepts the following request

POST /myulrpath/params?x=2,3
Host: somehost:20200
Content-Type: application/json

There is no payload allowed in the requst but it still requires the correct content/type. Im tryng to solve this using angular $resource but the content type is always set to allow all "*/*" once the request is sent. Anyone know if its possible to fix this using $resource or do I have to fall back to $http?

My 2 different approaches (none working)

var url = utils.prefixApiUrl('/myulrpath/params?x=:ids');
var parameters = 
var actions = {
    post: {
        method: 'post',
        hasBody: false,
        headers: {
            'Content-Type': 'application/json'
        },                        
        transformRequest: function (request, headers) {
            headers = {'Content-Type': 'application/json'};
        }
    }
};

$resource(url, parameters, actions).post({ids:'1,2,3'});

and also

var url = utils.prefixApiUrl('/myulrpath/params?x=:ids');
var parameters = 
var actions = {
    post: {
        method: 'post',
        hasBody: false,                     
        transformRequest: function (request, headers) {
            headers = {'Content-Type': 'application/json'};
        }
    }
};

$resource(url, parameters, actions, ).post({ids:'1,2,3'});

Solution

  • So i figured out the answer and just wanted to share it.

    var url = utils.prefixApiUrl('/myulrpath/params?x=:ids');
    var parameters = 
    var actions = {
        post: {
            method: 'post',
            headers: {
                      'Content-Type': 'application/json'
            },
        }
    };
    
    $resource(url, parameters, actions, ).post({ids:'1,2,3'}, null);
    

    So i removed the hasBody option and in the call to the post method i added a null as payload. This makes it possible to post without payload but with content-header