StatusCode: 400, ReasonPhrase: 'Bad Request'

Softnames Corporations
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
May 31, 2023

Hi,

 

I am trying to create issue using C# but getting this error:

StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.NSUrlSessionHandler+NSUrlSessionDataTaskStreamContent, Headers:
{
nel: {"failure_fraction": 0.001, "include_subdomains": true, "max_age": 600, "report_to": "endpoint-1"}
Server: AtlassianEdge
x-arequestid: bc94f0055967bc138de24fb5824e9bab
x-aaccountid: 63d6e1b6491b20ef64b76545
X-XSS-Protection: 1; mode=block
timing-allow-origin: *
atl-traceid: 883e1d9ad4a71061
Cache-Control: no-cache, no-store, no-transform
Date: Wed, 31 May 2023 19:17:44 GMT
report-to: {"endpoints": [{"url": "https://dz8aopenkvv6s.cloudfront.net"}], "group": "endpoint-1", "include_subdomains": true, "max_age": 600}
Strict-Transport-Security: max-age=63072000; includeSubDomains; preload
X-Content-Type-Options: nosniff
Set-Cookie: atlassian.xsrf.token=181df472-89ab-44b9-aaa1-c4468df68ef6_5d540690f3339243bbf99a814c759c7c07067a13_lin; Path=/; Domain=softnames.atlassian.net; Version=0; Discard; Secure
Content-Type: application/json;charset=UTF-8
nel: {"failure_fraction": 0.001, "include_subdomains": true, "max_age": 600, "report_to": "endpoint-1"}
x-arequestid: bc94f0055967bc138de24fb5824e9bab
x-aaccountid: 63d6e1b6491b20ef64b76545
X-XSS-Protection: 1; mode=block
timing-allow-origin: *
atl-traceid: 883e1d9ad4a71061
report-to: {"endpoints": [{"url": "https://dz8aopenkvv6s.cloudfront.net"}], "group": "endpoint-1", "include_subdomains": true, "max_age": 600}
Strict-Transport-Security: max-age=63072000; includeSubDomains; preload
X-Content-Type-Options: nosniff
}

 

this is my code:

 

public static async Task CreateJiraIssueWithApiToken()
{
// Jira REST API endpoint for creating an issue
string apiUrl = "https://softnames.atlassian.net/rest/api/3/issue/";

// Jira authentication credentials
string username = "my@domain.com";
string apiToken = "xxxxxxxxx";

// Create the JSON payload for the issue
string payload = @"{
""fields"": {
""project"": {
""key"": ""MUFFIN""
},
""summary"": ""Test Issue"",
""description"": ""This is a test issue"",
""issuetype"": {
""name"": ""Bug""
}
}
}";

using (HttpClient client = new HttpClient())
{
// Set the API token as the password for basic authentication
string auth = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{username}:{apiToken}"));
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", auth);

// Set the request content type to JSON
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

// Send the POST request to create the issue
var response = await client.PostAsync(apiUrl, new StringContent(payload, Encoding.UTF8, "application/json"));

// Check the response status
if (response.IsSuccessStatusCode)
{
// Issue created successfully
Console.WriteLine("Issue created successfully.");
}
else
{
await Clipboard.SetTextAsync(response.ToString());

// Error creating the issue
Console.WriteLine("Failed to create the issue. Error: " + response.StatusCode);
}
}
}



 

 

2 answers

0 votes
Saber aliwd
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
June 22, 2024

public static async Task CreateJiraIssueWithApiToken()

{

// Jira REST API endpoint for creating an issue

string apiUrl = "https://softnames.atlassian.net/rest/api/3/issue/";

 

// Jira authentication credentials

string username = "my@domain.com";

string apiToken = "xxxxxxxxx";

 

// Create the JSON payload for the issue

string payload = @"{

""fields"": {

""project"": {

""key"": ""MUFFIN""

},

""summary"": ""Test Issue"",

""description"": ""This is a test issue"",

""issuetype"": {

""name"": ""Bug""

}

}

}";

 

using (HttpClient client = new HttpClient())

{

// Set the API token as the password for basic authentication

string auth = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{username}:{apiToken}"));

client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", auth);

 

// Set the request content type to JSON

client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

 

// Send the POST request to create the issue

var response = await client.PostAsync(apiUrl, new StringContent(payload, Encoding.UTF8, "application/json"));

 

// Check the response status

if (response.IsSuccessStatusCode)

{

// Issue created successfully

Console.WriteLine("Issue created successfully.");

}

else

{

await Clipboard.SetTextAsync(response.ToString());

 

// Error creating the issue

Console.WriteLine("Failed to create the issue. Error: " + response.StatusCode);

0 votes
David Bakkers
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 1, 2023

Hello @Softnames Corporations 

As per the example shown in the v3 Create Issue endpoint documentation, you must format the description field using ADF.

If you want to format the description field using plain text / Wiki markdown, you must use the old v2 Create Issue endpoint.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events