AngularJS and the external API - make it work like PostMan

I am trying to access the sqwiggle API inside my Angular application and have tested it using Postman . Everything seems to be working fine. I define the Basic Auth header, make a GET request to the endpoint of the message, and get a nice response. These are the request headers (from Postman):

Request URL:https://api.sqwiggle.com/messages
Request Method:GET
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,nl;q=0.6
Authorization:Basic [secret]
Cache-Control:no-cache
Connection:keep-alive
Host:api.sqwiggle.com
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.70 Safari/537.36

Now in my AngularJS application, I want to do the same:

$http.get('https://api.sqwiggle.com/messages', {
    headers: {
        Authorization: 'Basic [secret]'
    }
}).success(function(e) {
    alert(e);
});

But this leads to the following request headers:

Request URL:https://api.sqwiggle.com/messages
Request Method:OPTIONS
Status Code:404 Not Found
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,nl;q=0.6
Access-Control-Request-Headers:accept, authorization
Access-Control-Request-Method:GET
Connection:keep-alive
Host:api.sqwiggle.com
Origin:http://localhost:8888
Referer:http://localhost:8888/
User-Agent:Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X; en-us) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53

Given the fact that Postman uses JS to retrieve data from the API, should this be possible in AngularJS, or am I missing something here? Why does Angular convert a GET to an OPTIONS request and why doesn't it add an authorization header?

thanks for the help

+4
1

- $http.get

$http({
    method: "get",
    url: "https://api.sqwiggle.com/messages",
    headers: {
        'Authorization' : 'Basic secret'
    }
}).success(function(data) {
    console.log(data);
});
+1

Source: https://habr.com/ru/post/1525777/


All Articles