简体   繁体   English

如何将获取发布请求发送到基于 API 密钥的授权端点?

[英]How to send fetch post request to an API key based authorized endpoint?

I have a .netcore web API application with several endpoints and I have a simple UI built to access an endpoint with a button click using javascript fetch API. I have a .netcore web API application with several endpoints and I have a simple UI built to access an endpoint with a button click using javascript fetch API.

 fetch('api/Sessions', {
        method: 'POST',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        body: JSON.stringify(callData)
    })
    .then(response => response.text())
    .then((response) => updateResponse(response))
    .catch(error => console.error(error));
}

Now the API endpoint in the controller is authenticated with API-KEY, [ServiceFilter(typeof(AuthorizeKey))] I can no longer access the endpoint from my UI.现在 controller 中的 API 端点已使用 API-KEY 进行身份验证, [ServiceFilter(typeof(AuthorizeKey))]我无法再从我的 UI 访问端点。 How can I change the HTML/javascript code in order to send the post request from my UI to the authenticated endpoint?如何更改 HTML/javascript 代码以便将发布请求从我的 UI 发送到经过身份验证的端点? Thanks谢谢

Please correct me if I'm wrong.如果我错了,请纠正我。

fetch('api/Sessions', {
        method: 'POST',
        headers: {
            'X-API-KEY': 'apikey',
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        body: JSON.stringify(callData)
    })
    .then(response => response.text())
    .then((response) => updateResponse(response))
    .catch(error => console.error(error));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM