代码之家  ›  专栏  ›  技术社区  ›  mornindew

大摇大摆地写下回应

  •  0
  • mornindew  · 技术社区  · 7 年前

    WriteResponse(rw http.ResponseWriter, producer runtime.Producer)
    

    我遇到的问题是,我不知道如何将字符串转换为http.ResponseWriter文件创建一个运行时生产者.

    如果有帮助的话,这里是我的代码片段。。。

    //convert the database response to a string array of valid JSON
    stringArray, conversionError := plan.ConvertPlanArrayToString(response)
    if conversionError != nil {
        return swaggerPlan.NewPlanGetTemplatePlanInternalServerError()
    }
    
    //Need to convert string value
    stringValue := stringArray[0]
    return swaggerPlan.NewPlanGetTemplatePlanOK().WriteResponse(NOT SURE HOW TO CREATE http.ResponseWriter, runtime.Producer)
    

    非常感谢你

    2 回复  |  直到 7 年前
        1
  •  0
  •   Kevin Sandow    7 年前

    正如Flimzy所提到的,您必须将字符串写入 http.ResponseWriter ,这可能是你 WriteResponse 不管怎样,函数还是可以。

    您提供的代码应该在某个处理程序函数中调用:

    func (...) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
      // your code should be here and you can pass the http.ResponseWriter
    }
    
        2
  •  0
  •   mornindew    7 年前

    我的处理程序被更新为返回一个自定义的“ResponderFunc”,然后我就可以直接将输出写入它。很好,这是我的密码。。。

    //Handle a 200 by sending the list of plans back as json
        return middleware.ResponderFunc(func(rw http.ResponseWriter, pr runtime.Producer) {
            rw.WriteHeader(200)
            rw.Write(byteArrayPlan)
        })
    
    推荐文章