Just as Daniel said "You can't just open it in a browser. You have to provide a request body
"
So you can use tools such as Postman to send the request with request body, or you can use PowerShell to call the REST API with request body.
Besides, based on my test it seems the REST API you mentioned (Code Search Results) is not apply to on-premise TFS. I tested on TFS 2018 Update2 (Version 16.131.27701.1
), it always return "count": 0,
.
However you can use below REST API to search code:
POST http://server:8080/tfs/DefaultCollection/{Project}/_api/_search/postCodeQuery?api-version=4.1-preview.1
Request Body:
{"searchText":"<test1>",
"scope":"Team Foundation Server",
"filters":"{\"ProjectFilters\":[\"0511ScrumTFVC\"]}",
"skipResults":0,
"takeResults":50,
"sortOptions":""
}
Below PowerShell sample for your reference:
Param(
[string]$baseurl = "http://server:8080/tfs/DefaultCollection",
[string]$projectName = "ProjectName",
[string]$user = "username",
[string]$token = "password"
)
# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
function CreateJsonBody
{
$value = @"
{"searchText":"<test1>",
"scope":"Team Foundation Server",
"filters":"{\"ProjectFilters\":[\"ProjectName\"]}",
"skipResults":0,
"takeResults":50,
"sortOptions":""
}
"@
return $value
}
$json = CreateJsonBody
$uri = "$baseurl/$($projectName)/_api/_search/postCodeQuery?api-version=4.1-preview.1"
Write-Host $uri
$result = Invoke-RestMethod -Uri $uri -Method POST -Body $json -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
$result = $result | convertto-json
Write-host $result
![enter image description here]()