【问题标题】:How to remove charset=utf-8 in a Content-Type header, generated by grails如何在由 grails 生成的 Content-Type 标头中删除 charset=utf-8
【发布时间】:2019-09-19 05:59:21
【问题描述】:

我正在尝试将 json 数据作为 grails 中的响应正文发送。我尝试使用以下方法将 Content-Type 标头设置为 application/json:

render (status: httpServletResponse, text: responseToRender as JSON, contentType: "application/json")

每次生成的header如下:

Content-Type: application/json;charset=utf-8

如何去掉 charset=UTF-8 后缀?

【问题讨论】:

    标签: grails


    【解决方案1】:

    你无法摆脱字符集后缀。 您可以使用此处定义的 charset 参数对其进行更改:

    https://docs.grails.org/latest/ref/Controllers/render.html

    你也可以不提供任何信息,只交给json去渲染,比如:

    response.setContentType("application/json")
    render JsonOutput.toJson(responseToRender);
    

    但是,这将默认为 HTTP 1.1 要求的标准编码,即 ISO-8859-1。因此您的结果将是 application/json;charset=ISO-8859-1

    https://www.w3.org/International/articles/http-charset/index.en

    所以,如果你不知何故需要使用这个参数,你可以使用 .split(";")[0] 来只访问第一部分。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-12
      • 2015-08-24
      • 2011-06-09
      • 2021-06-22
      相关资源
      最近更新 更多