Error when transitioning a jira issue with content using the rest api in c#

John Lawlor May 2, 2024

I am trying to transition a jira ticket and update some fields as I go. I can transition the issue and not update any other fields and it only complains about 'no content' in the issue. But when I try to add content  get "Status Code 400 - Bad Request" error. 

This block of code is what I successfully use to transition the issue with no content

   

public async void updateTicket()

string BaseUrl = "https://jira";
string Username = "user";
string Password = "pas";
string restUrl = String.Format("https://jira/rest/api/2/issue/{0}/transitions?expand=transitions.fields", "ABC-12345");

using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("POST"), restUrl))
{
string base64Credentials = GetEncodedCredentials();
request.Headers.Authorization = new AuthenticationHeaderValue("Basic", base64Credentials);

// successfully updates ticket, but no content
var requestBody = new
{
transition = new
{
id = "11"
}
};

var jsonRequestBody = Newtonsoft.Json.JsonConvert.SerializeObject(requestBody);
request.Content = new StringContent(jsonRequestBody);
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var response = await httpClient.SendAsync(request);

if (response.StatusCode == HttpStatusCode.OK)
{
Console.WriteLine("Pass");
}
else
{
Console.WriteLine($"Failed to update transition. Response: {response}");
Console.WriteLine($"Failed to update transition. Status code: {response.StatusCode}");
Console.WriteLine($"Failed to update transition. RequestMessage: {response.RequestMessage}");
}
}
}

But when I try to update a field all attempts fail with "Status Code 400 - Bad Request"
    // returns status code 400 - bad request
    var requestBodyFull = new
    {
        transition = new
        {
        id = "11"
        },
        fields = new
        {
        customfield_18421 = "Yes",
        }
   };
    var jsonRequestBody = Newtonsoft.Json.JsonConvert.SerializeObject(requestBodyFull);
    request.Content = new StringContent(jsonRequestBody);
    
// returns status code 400 - bad request
    var jsonRequestBody = Newtonsoft.Json.JsonConvert.SerializeObject(@"{""transition"":{""id"":""11""},""fields"":{""customfield_18421"":""Yes""}}");
    request.Content = new StringContent(jsonRequestBody);
    
// returns status code 400 - bad request
    request.Content = new StringContent(@"{""transition"":{""id"":""11""},""fields"":{""customfield_18421"":""Yes""}}", Encoding.UTF8, "application/json")

 

 

0 answers

Suggest an answer

Log in or Sign up to answer