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