1
votes

I am using TFS2018 and calling the build api like this

internal void UpdateSourceBranches(List<BuildDefinition> defs)
        {
            using (var handler = new HttpClientHandler { Credentials = new NetworkCredential(tfsUser, tfsPass) })
            using (var client = new HttpClient(handler))
            {
                try
                {
                    client.BaseAddress = new Uri(tfsServer);
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    foreach (var def in defs)
                    {
                        var buildId = def.Id;
                        var sourceBranch = $"$/{def.Repository.Name}/{def.Project.Name}";
                        var parameters = new Dictionary<string, string> { { "BuildConfiguration", "release" },
                        { "BuildPlatform", "x86|x64|ARM" },
                        { "system.debug", "true" }
                    };

                        var jsonParams = JsonConvert.SerializeObject(parameters);

                        var content = new FormUrlEncodedContent(new[]
                        {
                            new KeyValuePair<string, string>("id", buildId.ToString()),
                            new KeyValuePair<string, string>("sourceBranch", sourceBranch),
                            new KeyValuePair<string, string>("parameters", jsonParams)
                        });

                        var response = client.PostAsync($"DefaultCollection/{def.Repository.Name}/_apis/build/builds?api-version=3.0-preview.1", content);
                        var s = response.Result;
                    }                  
                }
                catch (Exception ex)
                {

                }

            }
        }

But getting the following response

{StatusCode: 405, ReasonPhrase: 'Method Not Allowed', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Pragma: no-cache X-TFS-ProcessId: ActivityId: X-TFS-Session: X-VSS-E2EID: X-FRAME-OPTIONS: SAMEORIGIN X-VSS-UserData: :user Persistent-Auth: true Lfs-Authenticate: NTLM X-Content-Type-Options: nosniff Cache-Control: no-cache Date: Fri, 09 Mar 2018 14:37:16 GMT P3P: CP="CAO DSP COR ADMa DEV CONo TELo CUR PSA PSD TAI IVDo OUR SAMi BUS DEM NAV STA UNI COM INT PHY ONL FIN PUR LOC CNT" Server: Microsoft-IIS/10.0 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Content-Length: 93 Allow: GET Content-Type: application/json; charset=utf-8 Expires: -1 }}

Any idea why I am getting Method Not Allowed ?

Here is the bad response:

{StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Pragma: no-cache
X-TFS-ProcessId: xxxxxx-xxxxx-xxxxx-xxxxxx-xxxxxxxxxx ActivityId: xxxxxx-xxxxx-xxxxx-xxxxxx-xxxxxxxxxx X-TFS-Session: xxxxxx-xxxxx-xxxxx-xxxxxx-xxxxxxxxxx X-VSS-E2EID: xxxxxx-xxxxx-xxxxx-xxxxxx-xxxxxxxxxx X-FRAME-OPTIONS: SAMEORIGIN
X-VSS-UserData: xxxxxx-xxxxx-xxxxx-xxxxxx-xxxxxxxxxx:user
Persistent-Auth: true Lfs-Authenticate: NTLM
X-Content-Type-Options: nosniff Cache-Control: no-cache Date: Tue, 13 Mar 2018 09:21:53 GMT P3P: CP="...multiple keywords" Server: Microsoft-IIS/10.0 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Content-Length: 547 Content-Type: application/json; charset=utf-8 Expires: -1 }}

2

2 Answers

1
votes

Sounds like your POST is the issue, try using a GET

1
votes

You should use api-version=2.0 instead of 3.0-preview.1.

Update code snippet:

string con = "{\"definition\": {\"id\": 47},\"sourceBranch\":\"$/CeceScrum/TestCaseProject\",\"parameters\":\"{\\\"BuildConfiguration\\\":\\\"release\\\",\\\"BuildPlatform\\\":\\\"any cpu\\\",\\\"system.debug\\\":\\\"false\\\"}\"}";
                        var patchValue = new StringContent(con, Encoding.UTF8, "application/json");
                        HttpClient httpClient = new HttpClient();
                        string _credentials = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "domain\\username", "password")));
                        httpClient.DefaultRequestHeaders.Accept.Clear();
                        httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _credentials);
                        var method = new HttpMethod("POST");

                        var request = new HttpRequestMessage(method, "http://TFS2018:8080/tfs/DefaultCollection/CeceScrum/_apis/build/builds?api-version=2.0") { Content = patchValue };

                        httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                        HttpResponseMessage response = httpClient.SendAsync(request).Result;

                       string re= response.Content.ReadAsStringAsync().Result;