我正在使用JIRA REST API来创建一个新问题,并且在描述和一些其他自定义字段中有一些非英语字母表.请求JSON看起来像
{
"fields": {
"issuetype": {
"id": 10303
},
"description": " Additional informations",
"customfield_11419": "",
"customfield_11413": "Editor: Øyst gården",
"customfield_11436": {
"value": "DONE"
},
"customfield_11439": "Jørund"
}
}
Run Code Online (Sandbox Code Playgroud)
使用以下代码完成HTTP POST后,我从端点返回OK响应.
HttpWebRequest request;
WebResponse response;
request = WebRequest.Create(jira_url) as HttpWebRequest;
request.Credentials = CredentialCache.DefaultCredentials;
request.Method = "POST";
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
byte[] authBytes = Encoding.UTF8.GetBytes((jira_email + ":" + jira_token).ToCharArray());
request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(authBytes);
if (!string.IsNullOrEmpty(json_string)) //the inpot JSON string to be submitted
{
request.ContentType = "application/json; charset=utf8";
byte[] jsonPayloadByteArray = Encoding.ASCII.GetBytes(json_string.ToCharArray());
request.GetRequestStream().Write(jsonPayloadByteArray, 0, jsonPayloadByteArray.Length);
}
response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
response_string = reader.ReadToEnd();
reader.Dispose();
Run Code Online (Sandbox Code Playgroud)
但是在JIRA界面中渲染细节时我可以看到一些?替换特殊的非英文字符.例
"编辑:Øystgården"从JSON到
编辑:?用户界面中的用户界面
我们怎样才能避免?并确保非英文字母被发布到端点
似乎你使用了错误的编码/解码类型.
如果您决定使用UTF-8,则不会使用ASCII
所以在这里,
if (!string.IsNullOrEmpty(json_string)) //the inpot JSON string to be submitted
{
request.ContentType = "application/json; charset=utf8";
byte[] jsonPayloadByteArray = Encoding.ASCII.GetBytes(json_string.ToCharArray());
request.GetRequestStream().Write(jsonPayloadByteArray, 0, jsonPayloadByteArray.Length);
}
Run Code Online (Sandbox Code Playgroud)
你需要改变它
if (!string.IsNullOrEmpty(json_string)) //the inpot JSON string to be submitted
{
request.ContentType = "application/json; charset=utf8";
byte[] jsonPayloadByteArray = Encoding.UTF8.GetBytes(json_string.ToCharArray());
request.GetRequestStream().Write(jsonPayloadByteArray, 0, jsonPayloadByteArray.Length);
}
Run Code Online (Sandbox Code Playgroud)