为什么此vsts rest API返回404? [英] why is this vsts rest api returning 404?
问题描述
我遵循以下示例使用VSTS REST API:
I was following this example to use the VSTS REST API:
以下网址直接指向组织的VSTS中的MyService.MyController:
The following url points directly to MyService.MyController in my org's VSTS:
我尝试在下面的示例代码中添加示例代码,但返回了404响应,但没有详细的消息.知道问题可能是什么或如何调试?
I tried to follow the example code with my implentation code below but a 404 response is returned without a detailed message. Any idea what the issue might be or how to debug?
private static async Task FindMyControllerRefs()
{
var baseUrl = "https://my-corp.almsearch.visualstudio.com";
using (var client = GetHttpClient(baseUrl))
{
var request = "{ \"searchText\": \"MyController\" }";
var projectUri = "/USOpsInfrastructure/USOpsInfrastructure%20Team/_git/MyService";
var searchUri = "/_apis/search/codesearchresults?api-version=4.1-preview.1";
var fullUri = projectUri + searchUri;
var response = await client.PostAsJsonAsync(fullUri, request);
//process the response
if (response.IsSuccessStatusCode)
{
var result = (await response.Content.ReadAsAsync<string>());
}
else //not 200
{
var message = await response.Content.ReadAsStringAsync();
}
}
}
private static HttpClient GetHttpClient(string baseUrl)
{
var client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true });
client.BaseAddress = new Uri(baseUrl);
client.Timeout = Timeout.InfiniteTimeSpan;
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
return client;
}
推荐答案
代码搜索REST API应该为:
The Code search REST API should be :
POST https://XXX.almsearch.visualstudio.com/{Project}/_apis/search/codesearchresults?api-version=4.1-preview.1
在您的情况下,请确认团队项目名称是什么:USOpsInfrastructure
或MyService
.如果不确定,则可以尝试其中的每一个.
In you scenario please confirm what's the team project name: USOpsInfrastructure
or MyService
. If you are not sure about that, then you can have a try for each of them.
此外,根据我的测试,您需要在请求正文中添加"$top": 10
(您可以根据需要更改数字):
Besides, base on my test you need to add the "$top": 10
(you can change the number as needed) in the request body:
var request = "{ \"searchText\": \"MyController\",\"$top\": 10 }";
下面的代码对我有效:(以下示例中的项目名称为" Git ")
Below code works on my side: (Project name is "Git" in below sample)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace CodeSearch
{
class Program
{
public static void Main()
{
Task t = CodeSearch();
Task.WaitAll(new Task[] { t });
}
private static async Task CodeSearch()
{
try
{
var username = "username";
var password = "Password/PAT";
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(
new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(
System.Text.ASCIIEncoding.ASCII.GetBytes(
string.Format("{0}:{1}", username, password))));
string url = "https://{instance}.almsearch.visualstudio.com/Git/_apis/search/codesearchresults?api-version=4.1-preview.1";
var content = new StringContent("{\"searchText\": \"OwinStartupAttribute\", \"$top\": 10}", Encoding.UTF8, "application/json");
using (HttpResponseMessage response = client.PostAsync(url, content).Result)
{
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
Console.ReadLine();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.ReadLine();
}
}
}
这篇关于为什么此vsts rest API返回404?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!