Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Why do I keep getting Error 400 even I have correct Endpoint, JSON data and HTTP Request?

OliverCesumission
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!
April 7, 2020

I keep getting error 400. And as far as I know i have correct code. Here's my code. I'm using C#

 

URL: http://<company_url>/rest/api/2/issue

 

public string makeRequest()
{
HttpWebResponse response = null;


//ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
string strResponse = string.Empty;

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endPoint);
request.UserAgent = userName; //explicit definition
request.Method = httpMethod.ToString();
request.ClientCertificates = new System.Security.Cryptography.X509Certificates.X509CertificateCollection();
request.ServerCertificateValidationCallback += (se, cert, chain, sslerror) => { return true; };

 


String authHeader = System.Convert.ToBase64String(System.Text.UTF8Encoding.UTF8.GetBytes(userName + ":" + userpassWord));
request.Headers.Add("Authorization", "Basic " + authHeader);

if (request.Method == "POST" && postJSON != string.Empty)
{
request.ContentType = "application/json";
using (StreamWriter swJSONPayLoad = new StreamWriter(request.GetRequestStream()))
{
//string temp_json = @"{"fields": {"issueType": {"name": "Task"},"project": {"key": "TA"},"assignee": {"name": "OliverCesumission"},"reporter": {"name": "OliverCesumission"}}}";
swJSONPayLoad.Write(postJSON);
swJSONPayLoad.Close();
}
}

try
{
response = (HttpWebResponse)request.GetResponse();

//Process the reponse stream (it could be JSON, XML, HTML)

using (Stream responseStream = response.GetResponseStream())
{
if (responseStream != null)
{
using (StreamReader reader = new StreamReader(responseStream))
{
strResponse = reader.ReadToEnd();
}
}
}
}
catch (System.Net.WebException ex)
{

strResponse = ex.ToString();
}

finally
{
if (response != null)
{
((IDisposable)response).Dispose();
}
}

return strResponse;
}

 

 

HERE IS MY JSON DATA:

 

{
"fields": {
"project": {
"id": "11518"
},
"issuetype": {
"id": "10203"
},
"summary": "[TA] REST API POST",
"description": "TEST ISSUE ONLY",
"reporter": {
"name": "OliverCesumission"
},
"assignee": {
"name": "OliverCesumission"
}
}
}

0 answers

Suggest an answer

Log in or Sign up to answer