douqie1884 2019-06-17 15:31
浏览 189
已采纳

POST请求有效内容的内容类型

I am sending a JSON body in my POST Request, but http.DetectContentType is identifying it as a text/plain type.

I want to have flexibility to process request payload based on their content type - if XML {} if JSON {} else {NO Processing}

To achieve this conditional processing, I am using http.DetectContentType to return the content type o the request but it is returning text/plain is every scenario.

func Test(w http.ResponseWriter, r *http.Request) *ErrorObject {

        reqBuffer := make([]byte, 512)
        _, err := r.Body.Read(reqBuffer)
        if err != nil {

    return ErrorObject{}.New(1, err, nil)
}

contentType := GetContentType(reqBuffer)
fmt.Printf(contentType)

    if contentType == "application/xml" || contentType == "text/xml" {
    w.Header().Set("Content-Type", "application/xml; charset=UTF-8") ...}
    if contentType == "application/json" || contentType == "text/json" {
    w.Header().Set("Content-Type", "application/json; charset=UTF-8") ... } 
    else return Invalid Request Type error
} 

   func GetContentType(buffer []byte) string {

       fmt.Println(string(buffer))
       contentType := http.DetectContentType(buffer)
       fmt.Printf(contentType)
       return contentType

    }

Expect to return the function - Content Type as application/json but getting text/plain

Using POSTMAN to send request to server with Body as raw and JSON

    {
      "data": [
         {
           "group": "TEST",
           "name": "TEST",
           "released": true,
           "version": 1,
           "teststeps": [
              {
                   "bin": 32,
                   "comment": "PAA",
                   "dataType": "J",
                   "format": "R6.2",
                   "id": "PAA3",
                   "osg": 8,
                   "usg": 0
              }
            ],
           "parameters": [
              {
                  "comment": "test",
                  "description": "test",
                  "format": "R7.0",
                  "id": 1,
                  "teststepId": "PAA",
                  "value": 30,
                  "type": "teststep"
            }
          ]
        }
     ]
  }
  • 写回答

1条回答 默认 最新

  • dql123000 2019-06-17 15:42
    关注

    I am using http.DetectContentType to return the content type o the request but it is returning text/plain is every scenario.

    According to the documentation DetectContentType "... implements the algorithm described at https://mimesniff.spec.whatwg.org/ to determine the Content-Type of the given data". The algorithm there is primarily for handling content types which the browser can handle by itself.

    And if you look at the actual code you'll see that it does not care about application/json or similar at all and that it returns text/plain for anything which looks non-binary (and was not matched before as with text/html).

    In other words: this is the wrong tool for the job. The proper way would be for the client to specify what kind of content is sent using the Content-Type header and not to let the server guess the kind of content.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c#里的静态变量为什么是错误的?
  • ¥15 服务器安装php5.6版本
  • ¥15 我想用51单片机和数码管做一个从0开始的计数表 我写了一串代码 但是放到单片机里面数码管只闪烁一下然后熄灭
  • ¥20 系统工程中,状态空间模型中状态方程的应用。请猛男来完整讲一下下面所有问题
  • ¥15 我想在WPF的Model Code中获取ViewModel Code中的一个参数
  • ¥15 arcgis处理土地利用道路 建筑 林地分类
  • ¥20 使用visual studio 工具用C++语音,调用openslsx库读取excel文件的sheet问题
  • ¥100 寻会做云闪付tn转h5支付链接的技术
  • ¥15 DockerSwarm跨节点无法访问问题
  • ¥15 使用dify通过OpenAI 的API keys添加OpenAI模型时报了“Connection Error”错误