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);
}
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'}}}";
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.