Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
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

C# .NET upload custom field options -> The remote server returned an error: (415) Unsupported Media

Edited

This doesn't seem to happen when creating a custom field, but it does when I am creating the custom field options.

The string value of the json I am uploading: 

 

{"options":[{"value":"value 0","disabled":false},{"value":"value 1","disabled":false},{"value":"value 2","disabled":false},{"value":"value 3","disabled":false},{"value":"value 4","disabled":false},{"value":"value 5","disabled":false},{"value":"value 6","disabled":false},{"value":"value 7","disabled":false},{"value":"value 8","disabled":false},{"value":"value 9","disabled":false}]}
Uploading via postman has no issues, however there is some conversion issue with uploading the same values through WebClient as either UTF-8 or ASCII.
It seems like the JIRA API cant reconstruct the bytes, and assumes its a foreign media type.
Here is my code:

WebClient client = new WebClient();
string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes("<user>" + ":" + "<token>"));
client.Headers[HttpRequestHeader.Authorization] = string.Format("Basic {0}", credentials);
client.Headers.Set("Content-Type", "application/json");
client.Headers.Set("Accept", "application/json");

////////////////////////////////////////////

Here I upload the new Custom Field, and retrieve its context (Working)

///////////////////////////////////////////

//now that we have the context, it is time to update the options

//Creating a whole bunch of option values from 0-9
dynamic optionsObj = new JObject();
dynamic optionsArr = new JArray();

for (int i = 0; i < 10; i++) {
dynamic j = new JObject();
j.value = $"value {i}";
j.disabled = false;
optionsArr.Add(j);
}
optionsObj.options = optionsArr;


string oString = JsonConvert.SerializeObject(optionsObj, Formatting.None, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
dynamic oObj = JToken.Parse(oString);


string oObjstring = JsonConvert.SerializeObject(oObj, Formatting.None, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });

Console.WriteLine("\n Options to be sent: \n");


Console.WriteLine(oObjstring);


Console.WriteLine();


//var stringContent = new System.Net.Http.StStringContent(json, UnicodeEncoding.UTF8, "application/json");

byte[] oObjByte = Encoding.ASCII.GetBytes(oString);

string uploadOp = $"https://<name>.atlassian.net/rest/api/2/field/{jCreated.id}/context/{Contextid}/option";

//byte[] responseArray2 = client.UploadData(uploadOp, oObjByte);

string responseText = client.UploadString(uploadOp, oObjstring);

string returned2 = Encoding.ASCII.GetString(responseArray);

Console.WriteLine(returned2);


Console.ReadLine();

1 answer

1 accepted

1 vote
Answer accepted

All good guys, I figured it out after spending some time out with the kids today.

Decided to use HttpWebRequest Class instead.

Used the following, perhaps it had something to do with the wrong encoder. FYI none of this code is mine, was able to draw some inspiration from stackoverflow to pull it together.

I take zero credit for what some of the other coding giants out there are able to accomplish.

public static string Post(string uri, string data, string contentType, string method = "POST")
{
byte[] dataBytes = Encoding.UTF8.GetBytes(data);

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
request.ContentLength = dataBytes.Length;
request.ContentType = contentType;
request.Method = method;

string encoded = System.Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1")
.GetBytes("<user>" + ":" + "<token>"));
request.Headers.Add("Authorization", "Basic " + encoded);


using (Stream requestBody = request.GetRequestStream())
{
requestBody.Write(dataBytes, 0, dataBytes.Length);
}

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
TAGS
Community showcase
Published in Jira Software

👋 Looking for 15-20 volunteers to test Atlassian training content

Hi everyone! Are you interested in beta testing Atlassian University’s newest (unreleased!) training course? We’re looking for 15-20 volunteers to test our newest training course, Basic reporting...

394 views 19 22
Read article

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