1

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.

2
  • 1
    Have you looked at what the browser is actually sending using your browser's devtools? Commented Feb 15, 2013 at 13:49
  • Good point, I hadn't checked it but the data was being sent Commented Feb 15, 2013 at 22:48

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

That's done the trick, thanks. Not sure why jQuery was working when AngularJS wasn't though. The AJAX methods look so simialr
@Skeater I'd say you used jQuerys .serialize which would work with parse_str as it produces the right format.
is there any way to get $http.put to not send a json object, and send the correctly formatted object?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.