使用REST API发布非英语字母

时间:2018-10-04 06:04:54

标签: c# jira-rest-api

我正在使用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"
      }
    }

使用以下代码完成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();

但是在JIRA界面中渲染细节时,我可以看到其中一些吗?替换特殊的非英语字符。 例子

“编辑器:Øystgården”从JSON到

编辑器:用户界面中的yst g?rden

如何避免?并确保将非英语字母发布到端点

1 个答案:

答案 0 :(得分:5)

似乎您使用了错误的编码/解码类型。

如果您决定使用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);
        }    

您需要将其更改为

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);
        }