Api 具有包含凭据的URL的请求

Api 具有包含凭据的URL的请求,api,reactjs,curl,fetch-api,Api,Reactjs,Curl,Fetch Api,我正在尝试获取一个curl并从一个API获取一个JSON curl -XPOST -d "grant_type=password" -d "username=admin@admin.admin" \ -d "password=admin" "web_app@localhost:8081/oauth/token" 当我在终端中使用curl时,一切都很好,但是尝试使用fetch时,我得到了底部提到的错误消息 fetch("http://web_app@localhost:8081/oaut

我正在尝试获取一个curl并从一个API获取一个JSON

curl -XPOST -d "grant_type=password" -d "username=admin@admin.admin" \
    -d "password=admin" "web_app@localhost:8081/oauth/token"
当我在终端中使用curl时,一切都很好,但是尝试使用fetch时,我得到了底部提到的错误消息

fetch("http://web_app@localhost:8081/oauth/token", {
        credentials: 'include',
        body: "grant_type=password&username=admin@admin.admin&password=admin",
        headers: {
            "Content-Type": "application/x-www-form-urlencoded",
        },
        method: "POST"
    }
这是我得到的错误:

TypeError:是带有 嵌入式凭证


我的提取错误吗?

您不能使用
https://user:pass@host.com
form,您需要设置授权http头:

var headers = new Headers();

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

其中,
btoa
将“用户:传递”编码为base64编码字符串。您可以在上找到
btoa
功能可用性(简而言之:它适用于IE 10及以上版本)。

这是什么原因?