400 Bad Request when using a PUT or POST via the REST API from .NET

Mark Everest June 11, 2012

Hi,

I am trying to update a version in a project using the REST api via .NET

I can sucessfully do GETs but PUTs and POSTs fail with a '400 bad request' error

Here is the code I am using - the json string has been hard-coded (and copied from the API documentation) until I can get this to work:

var request = WebRequest.Create("http://jira-devt:8080/rest/api/2/version");
request.Method = "POST";
request.ContentType = "application/json";

var json = "{\"description\": \"An excellent version\",\"name\": \"New Version 1\",\"userReleaseDate\": \"5-Jul-2010\",\"project\": \"ADAS\",\"releaseDate\": \"2010-07-05\",\"released\": true,\"archived\": false}";
var writer = new StreamWriter(request.GetRequestStream());
writer.Write(json);
writer.Close();

response = request.GetResponse();

Any ideas please?

Thanks

Mark

2 answers

1 accepted

0 votes
Answer accepted
Mark Everest June 12, 2012

Here is some code (in case anyone else wants to do this sort of thing :-) ):

var json = "{\"description\": \"An excellent version\",\"name\": \"New Version 1\",\"project\": \"ADAS\",\"releaseDate\": \"2010-07-05\",\"released\": true,\"archived\": false}";

var request = WebRequest.Create("http://jira:8080/rest/api/2/version");
request.Method = "POST";
request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes("admin:JiraAdminPasswordGoesInHere-OtherUsersAreAvailable...")));	// if you need to login
request.ContentType = "application/json";
request.GetRequestStream().Write(Encoding.ASCII.GetBytes(json), 0, json.Length);

var reader = new StreamReader(request.GetResponse().GetResponseStream());
var jsonResponse = reader.ReadToEnd();


// And if you have defined typed objects you can deserialise using the Newstonsoft.Json library ...
var version = JsonConvert.DeserializeObject<Version>(jsonResponse);

0 votes
Mark Everest June 12, 2012

Hey all,

It doesn't like both releaseDate and userReleaseDate beging specified at once!

Suggest an answer

Log in or Sign up to answer