2

I am trying to deploy a flask+angular app, where flask is hosted on a Digital Ocean ubuntu server.

The api is based on this guide.

In curl I can confirm it works by: curl -u <username>:<passwd> -i -X GET https://<server>

I try to obtain the same in angular using http:

$http.get('https://<server>', {username: <user>, password: <password>}) .then(function(response){ console.log(response.data});

Results in a 401 UNAUTHORIZED

I hope you can help me here.

DauleDK
  • 3,313
  • 11
  • 55
  • 98

1 Answers1

3

You can use post methods instead of get to send data

$http.post('https://<server>', 
    {username: <user>, password: <password>})
    .then(function(response){
        console.log(response.data);
    });

and in serve get by req.body.username. I assume server side is 'node.js' that's why used 'req.body'.

then also need post method in server side to accept your post request for your url.

OR

If you want to use get method then you can send data as a params like

var config = {
 params: {username: <user>, password: <password>},
 headers : {'Accept' : 'application/json'}
};

$http.get('https://<server>', config).then(function(response) {
   // process response here..
   console.log(response.data);
 });
Shaishab Roy
  • 16,335
  • 7
  • 50
  • 68
  • backend is flask, just like this [link](http://blog.miguelgrinberg.com/post/restful-authentication-with-flask) – DauleDK Apr 02 '16 at 11:26
  • for `Flask` get data using `request.json.get('username')` – Shaishab Roy Apr 02 '16 at 11:31
  • but is it not "wrong" to use a post only because of authentication, when I would use a GET without authentication? – DauleDK Apr 02 '16 at 11:52
  • Can use as you need but you can't send data using get method if you want to use get method you should send `data` as `params` not as data. I updated my answer @DauleDK – Shaishab Roy Apr 02 '16 at 12:30
  • Thanks a lot for the updated answer. Is it safe to use params, when using https? – DauleDK Apr 02 '16 at 14:16
  • as far I know the whole URL, and even the type of request (get, post, etc.) is encrypted when using HTTPS. but better to send `token` instead of `username` and `password` – Shaishab Roy Apr 02 '16 at 14:22