It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

I have error 400 in c# with API REST Edited

Hello everyone, see if anyone can help me. I want to create an issue from an application in c #, but it returns error 400. I have tested my Json on POSTMAN, and it creates the issue correctly, but in c # I have not managed to work, my code is as follows:

string json = @"{
                         'fields':{
                                     'project':{
                                                    'key':'SEExxx',
                                                    'projectTypeKey': 'service'
                                                   },
                                      'summary': 'Esto es una prueba APIREST',
                                      'issuetype': {
                                                        'name': 'SAAxxx'
                                                         }
                                       }
}";
var userid = "xxxx@domainexample.com";
var password = "gsfasdfsdfgexample";
var endpoint = @"https://xxxxxx.atlassian.net/rest/api/2/issue?";

PostJsonRequest(endpoint,userid,password,json);

/******************************************************************/

public static string PostJsonRequest(string endpoint, string userid, string password, string json)
{
// Create string to hold JSON response
string jsonResponse = string.Empty;

using (var client = new WebClient())
{
try
{
client.Encoding = System.Text.Encoding.UTF8;
client.Headers.Set("Authorization", "Basic " + GetEncodedCredentials(userid, password));
client.Headers.Add("Content-Type: application/json");
client.Headers.Add("Accept", "application/json");
var uri = new Uri(endpoint);
var response = client.UploadString(uri, "POST", json);
jsonResponse = response;
}
catch (WebException ex)
{
// Http Error
if (ex.Status == WebExceptionStatus.ProtocolError)
{
HttpWebResponse wrsp = (HttpWebResponse)ex.Response;
var statusCode = (int)wrsp.StatusCode;
var msg = wrsp.StatusDescription;
Console.WriteLine(statusCode + " " + msg);
}
else
{
Console.WriteLine(ex.Message);
}
}
}

return jsonResponse;
}

/**********************************************************************/

 

private static string GetEncodedCredentials(string userid, string password)
{
string mergedCredentials = string.Format("{0}:{1}", userid, password);
byte[] byteCredentials = UTF8Encoding.UTF8.GetBytes(mergedCredentials);
return Convert.ToBase64String(byteCredentials);
}

1 answer

1 accepted

Solved.

Changed the JSON in only one line and quote (")

 

string json = @"{\"fields\":{\"project\":{\"key\":\"SEExxx\",\"projectTypeKey\": \"service\"},\"summary\": \"Esto es una prueba APIREST\",\"issuetype\": {\"name\": \"SAAxxx'}}}";

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Jira

Demo Den Ep. 7: New Jira Cloud Reports

Learn how to use two new reports for next-gen projects in Jira Cloud:  Cumulative flow diagram and Sprint burndown chart. Ivan Teong, Product Manager, Jira Software, demos the Cumulative ...

275 views 1 2
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you