none
Queue vNext build from c# using REST API (Tfs on premise) RRS feed

  • Question

  • This work great:

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://v-tinmo-12r2:8080/tfs/DefaultCollection2015U2/ScrumStarain/_apis/build/builds?api-version=2.0"); request.Credentials = CredentialCache.DefaultNetworkCredentials; request.Method = "Post"; request.ContentType = "application/json"; Stream stream = request.GetRequestStream(); string json = "{\"definition\":{\"id\":1}}"; byte[] buffer = Encoding.UTF8.GetBytes(json); stream.Write(buffer, 0, buffer.Length); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Console.Write(response.StatusCode); using (var streamReader = new StreamReader(response.GetResponseStream())) { var result = streamReader.ReadToEnd(); } Console.Read();

    But when i'm trying to add parameters its not working.

    when I do the POST from powershell its work great but i'm not convert it to json before the call.

    $json = "{
        'parameters':  '{ \'param1\':  \'\test\""\',\'param2\':  \'\""test2\""\'}',
        'definition':  {'id':  231}
    }"

    $build = Invoke-RestMethod -Method Post -UseDefaultCredentials -ContentType application/json -Uri "http://tfs-app:8080/tfs/DefaultCollection/ALM/_apis/build/builds?api-version=2.0" -Body $json

    Why its doesn't work with C# web request?

    BTW, when I do the call from POSTMAN with this body:

    {"definition":{"id":231},"parameters":{"FileToCheckInFullPath":"test"}}

    I got:

    "{"$id":"1","innerException":null,"message":"This request expects an object in the request body, but the supplied data could not be deserialized.","typeName":"Microsoft.TeamFoundation.Build.WebApi.RequestContentException, Microsoft.TeamFoundation.Build2.WebApi, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a","typeKey":"RequestContentException","errorCode":0,"eventId":3000}"

    But when I change the word "parameters" to any other word its work, the build queued but with empty params.

    Anyone can help? 

    Thanks.

    Sunday, December 29, 2019 3:47 PM

Answers

  • Found the problem:

    string comment = "Changes by Automatic build";
    string filesharepath = @"\\\\\\\\fs1\\\\Departments2\\\\test";
    string json = "{\"definition\":{\"id\":231},\"parameters\":\"{\\\"Comment\\\":\\\"m_essage\\\",\\\"FileToCheckInFullPath\\\":\\\"f_location\\\"}\"}";
    Console.WriteLine(json);
    string content = json.Replace("m_essage", comment).Replace("f_location", filesharepath);
    byte[] buffer = Encoding.UTF8.GetBytes(content);

    A lot of slashes.

    Happy new year :)


    • Marked as answer by yarshar Tuesday, December 31, 2019 3:26 PM
    Tuesday, December 31, 2019 2:57 PM