我尝试使用POST方法将数据发送到JIRA,但是在UploadString()方法中收到错误400。
令牌正确,并且在POSTMAN中测试过的Json没问题,这是问题吗?
我已经待了好几天了,但我解决不了,有人可以帮我吗?谢谢
string json = @"{
'fields': {
'project': {
'key': 'EXAMPLE',
'projectTypeKey': 'service_exam'
},
'customfield_11283': {
'self': 'https://example.atlassian.net/rest/api/2/customFieldOption/14538',
'value': 'HELLO',
'id': '14538'
},
'issuetype': {
'self': 'https://example.atlassian.net/rest/api/2/issuetype/11835',
'id': '11835',
'description': '',
'name': 'EXTac',
'subtask': false
},
'summary': '-',
'customfield_10300': {
'requestType': {
'_expands': [
'field'
],
'id': '119',
'_links': {
'self': 'https://example.atlassian.net/rest/serviceexmapi/serviceexm/11/requesttype/119'
},
'name': 'ExmTick 40517',
'description': 'Register Peter Parker',
'helpText': '
'issueTypeId':'10835',
'groupIds': [ '24' ]
}
}
}
}";
然后使用ATLASSIAN JIRA中的凭据编写的mi代码,请求为:
string jsonResponse = string.Empty;
var userid = "user@example.com";
var password = "QLodasdTCdt7Exampledsfsdf";
var endpoint = "https://example.atlassian.net/rest/api/2/issue?";
using (var client = new WebClient())
{
try
{
client.Encoding = Encoding.UTF8;
client.Headers.Set("Authorization", "Basic " + GetEncodedCredentials(userid, password));
client.Headers.Add("Content-Type: application/json");
client.Headers.Add("Accept", "application/json");
var uri = new Uri(endpoint);
var response = client.UploadString(uri, "POST", json);
jsonResponse = response;
}
catch (WebException ex)
{
if (ex.Status == WebExceptionStatus.ProtocolError)
{
HttpWebResponse wrsp = (HttpWebResponse)ex.Response;
var statusCode = (int)wrsp.StatusCode;
var msg = wrsp.StatusDescription;
throw new HttpException(statusCode, msg);
}
else
{
throw new HttpException(500, ex.Message);
}
}
}
return jsonResponse;
}
UTF8中的转换方法是:
private static string GetEncodedCredentials(string userid, string password)
{
string mergedCredentials = string.Format("{0}:{1}", userid, password);
byte[] byteCredentials = UTF8Encoding.UTF8.GetBytes(mergedCredentials);
return Convert.ToBase64String(byteCredentials);
}