Pete
Pete

Reputation: 4622

AngularJS $http.put PUT Method not sending Data

I'm switching from jquery $.ajax, which was working fine, to using AngularJS $http.put to access a restful API.

I can make an API call, but the PUT data isn't getting sent - so my API sees a PUT request with an empty data object, which should contain a JSON string -> data.values = 'a json structure'

$http.put(
    $rootScope.api_url,
        {
            values: jsonifiedValues
        },
        {
        headers: {
            apihash: sha256hash
        }
    }).success(function(data,status,headers,config){
        // handle success
}).error(function(data,status,headers,config) {
        // handle failure
});

I've not used AngularJS's $http before, but when I dump out the data in my PHP api it's just empty. this is how I'm pulling it from the request in the PHP:

parse_str(file_get_contents('php://input'), $put_vars);  
$arr_req_data = $put_vars['values'];    

In my API if the apihash sent from the request doesn't match the sha256 hash built on the PUT values, it fails.

This is working in JQuery, just failing now I've switched to $http. I'm not sure why the PUT data seems to be empty.

Upvotes: 1

Views: 8567

Answers (1)

Yoshi
Yoshi

Reputation: 54649

The return value from file_get_contents('php://input') will be a JSON string (provided everything got sent), so parse_str is not the right function to handle that data.
Instead use json_decode.

Also there is no need to send jsonified values, it will just make things more complicated as you'll have to use json_decode twice.

Upvotes: 1

Related Questions