Restangular not setting headers on post

前端 未结 3 382
后悔当初
后悔当初 2021-01-27 19:40

I am trying to set the header for a single post request using restangular but the request is being sent as plain text instead of json. I have read the documentation here as wel

相关标签:
3条回答
  • 2021-01-27 20:21

    You can do:

    const headers = { headers: { 'Content-Type':'application/json' } };
    
    RestServ.wandem.withHttpConfig(headers).post(pip)
    
    0 讨论(0)
  • 2021-01-27 20:25

    After groping around the web, I found that restangular sends form data to the server in json format by default. I was misguided by the developer tools from chrome. In order to disect http requests I recommend fiddler If one wants to format the header in restangular being sent as a request from the form you can do the following:

    //From the previous example....
    //Assuming restangular did not not send form data in json format by default
    
        $scope.getWanPip = function(pip){
    
            RestServ.wandem.post(pip, {},{}, {'Content-Type':'application/json'}).then(function(res){
                console.log(res)      
            }, function(err){
                console.log('Error!!!\n', err);
            })
    

    Note the two empty curly braces instead of one... I can't still perform the post but this is because the data is being sent as part of the url instead of separately. This calls for another question...

    0 讨论(0)
  • 2021-01-27 20:31

    Don't pass two empty objects, try passing:

    .post(pip, undefined, undefined, {'Content-Type':'application/json'})
    

    Or even .customPOST() instead of .post().

    0 讨论(0)
提交回复
热议问题