Request with URL that includes credentials

前端 未结 1 958
闹比i
闹比i 2020-12-17 17:59

I\'m trying to fetch a curl and get a JSON from an API.

curl -XPOST -d \"grant_type=password\" -d \"username=admin@admin.admin\" \\
    -d \"password=admin\         


        
相关标签:
1条回答
  • 2020-12-17 18:38

    You can't use the https://user:pass@host.com form, you need to set the Authorization http Header:

    var headers = new Headers();
    
    headers.append('Authorization', 'Basic ' + btoa(username + ':' + password));
    fetch('https://host.com', {headers: headers})
    

    Where btoa encodes 'user:pass' to base64 encoded string. You can find btoa function availability on MDN (in short: it works on IE 10 and above).

    0 讨论(0)
提交回复
热议问题