I'm having quite a problem working with an API. I'm trying to lean the basics of requesting data.
The url for the test API is aq-test.000webhostapp.com/v1/ which returns a simple 'hello world', that works without problem.
Making a GET request to aq-test.000webhostapp.com/v1/users returns all users created (There's only one). This API PHP server has an authenticate function that checks if the header has an Authorization token whenever I make a request from it:
$headers = apache_request_headers();
$response = array();
$app = \Slim\Slim::getInstance();
// Verifying Authorization Header
if (isset($headers['Authorization'])) {
// get the api key
$token = $headers['Authorization'];
// validating api key
if (!($token == API_KEY)) { //Do Stuff}
My problem is, that I'm sending the header with the Autorization token, but it simply won't work. This is my code:
var settings = {
"async": true,
"crossDomain": true,
"url": "http://aq-test.000webhostapp.com/v1/users",
"method": "GET",
"headers": {
'Content-Type' : 'application/x-www-form-urlencoded',
'Authorization': '3d524a53c110e4c22463b10ed32cef9d'
}
}
$.ajax(settings).done(function (response) {
console.log(response);
});
No matter what I do, I always get bad requests. I even did it using AngularJS' $http.get sending the headers, but it didn't work.
It's strange, but if I use PostMan to make the request, ( PostMan file https://www.dropbox.com/s/3ms71lbnu1jb4wj/api_test.postman_collection?dl=0 ) it works flawlessly. In fact, my code is based in PostMan's request! What am I doing wrong here?
Hope you guys can enlighten me, because I´m clueless atm.