将C#语法HTTP调用转换为JS语法调用
Converting C# syntax HTTP call to JS syntax call
我得到了一个用 C# 实现的 HTTP 调用,它调用了 Azure API:
public async Task<string> CheckPipelineRunStatus(string pipelineId, string pipelineRunId, CancellationToken cancellationToken = default)
{
string responseBody = "";
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(
System.Text.ASCIIEncoding.ASCII.GetBytes(
string.Format("{0}:{1}", "", _token))));
var requestUrl = $"https://dev.azure.com/{_organization}/{_project}/_apis/pipelines/{pipelineId}/runs/{pipelineRunId}?api-version={_api_version}";
using (HttpResponseMessage response = await client.GetAsync(requestUrl, cancellationToken))
{
response.EnsureSuccessStatusCode();
responseBody = await response.Content.ReadAsStringAsync();
}
}
return responseBody;
}
我需要在 Javascript 调用中执行相同的调用,但我不确定应该如何发送 Authorization
header.
这是我到目前为止没有 header 的结果(代码中的评论中的问题):
async function checkPipelineStatus(url)
{
var params = {
method: 'GET',
headers: {
'Content-Type': 'application/json',
//is the auth supposed to be here? what to do with that 64base string?
}
};
const fetchResult = await fetch(url, params);
const result = await fetchResult.text();
if (!fetchResult.ok)
{
throw result;
}
return result;
}
根据要求,这是插入了 header 的 JavaScript 代码:
async function checkPipelineStatus(url)
{
let base64Credentials = btoa(':' + _token);
var params = {
method: 'GET',
headers: {
'Content-Type': 'application/json',
// Insert your own Base64 credentials here:
'Authorization': `Basic ${base64Credentials}`
}
};
const fetchResult = await fetch(url, params);
const result = await fetchResult.text();
if (!fetchResult.ok)
{
throw result;
}
return result;
}
我得到了一个用 C# 实现的 HTTP 调用,它调用了 Azure API:
public async Task<string> CheckPipelineRunStatus(string pipelineId, string pipelineRunId, CancellationToken cancellationToken = default)
{
string responseBody = "";
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(
System.Text.ASCIIEncoding.ASCII.GetBytes(
string.Format("{0}:{1}", "", _token))));
var requestUrl = $"https://dev.azure.com/{_organization}/{_project}/_apis/pipelines/{pipelineId}/runs/{pipelineRunId}?api-version={_api_version}";
using (HttpResponseMessage response = await client.GetAsync(requestUrl, cancellationToken))
{
response.EnsureSuccessStatusCode();
responseBody = await response.Content.ReadAsStringAsync();
}
}
return responseBody;
}
我需要在 Javascript 调用中执行相同的调用,但我不确定应该如何发送 Authorization
header.
这是我到目前为止没有 header 的结果(代码中的评论中的问题):
async function checkPipelineStatus(url)
{
var params = {
method: 'GET',
headers: {
'Content-Type': 'application/json',
//is the auth supposed to be here? what to do with that 64base string?
}
};
const fetchResult = await fetch(url, params);
const result = await fetchResult.text();
if (!fetchResult.ok)
{
throw result;
}
return result;
}
根据要求,这是插入了 header 的 JavaScript 代码:
async function checkPipelineStatus(url)
{
let base64Credentials = btoa(':' + _token);
var params = {
method: 'GET',
headers: {
'Content-Type': 'application/json',
// Insert your own Base64 credentials here:
'Authorization': `Basic ${base64Credentials}`
}
};
const fetchResult = await fetch(url, params);
const result = await fetchResult.text();
if (!fetchResult.ok)
{
throw result;
}
return result;
}