POST 使用 REST 的非英语字母表 API

POST Non English alphabets using 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 界面中渲染细节时我可以看到一些 ?替换特殊的非英文字符。 示例

"Editor: Øyst gården" 从 JSON 到

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

我们怎样才能避免?并确保将非英文字母发布到端点

您似乎使用了错误的 Encode/Decode 类型。

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