簡體   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.

 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));
}

現在 controller 中的 API 端點已使用 API-KEY 進行身份驗證, [ServiceFilter(typeof(AuthorizeKey))]我無法再從我的 UI 訪問端點。 如何更改 HTML/javascript 代碼以便將發布請求從我的 UI 發送到經過身份驗證的端點? 謝謝

如果我錯了,請糾正我。

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