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

Scott Mckennie January 7, 2022

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
Scott Mckennie January 8, 2022

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
AUG Leaders

Atlassian Community Events