在swagger go中工作POST的示例代码

时间:2017-03-06 17:47:16

标签: go swagger

这种做法我做错了什么?

我尝试重新实现一个旧的API,但是go实现。基本上我尝试将一个字符串列表发送到服务器。但我得到的是来自服务器的错误500,当我尝试时:

go run gen/cmd/greeter-server/main.go --port=3000  &
echo '["a","b"]' |  http -v post  :34307/api/greeting/2.0/revisit

RESULT

HTTP/1.1 500 Internal Server Error
Content-Length: 68
Content-Type: application/json
Date: Mon, 06 Mar 2017 17:41:16 GMT

{
    "code": 500,
    "message": "no consumer registered for application/json"
}

这是我尝试实施的代码。

  api.TrackingRevisitHandler = operations.TrackingRevisitHandlerFunc(
                func(params operations.TrackingRevisitParams) middleware.Responder {

                        list := params.IdList
                        log.Printf("%v\n",list)
                        return operations.NewTrackingRevisitOK().WithPayload("RESULT")
        })

我用

swagger generate server -A greeter -t gen -f ./swagger/swagger.yml

从这个yaml生成服务器:

---
swagger: '2.0'
info:
  version: 1.0.0
  title: Greeting Server

basePath: "/api/greeting/2.0"

paths:
  /revisit:
    post:
      produces:
      - application/json
      consumes:
      - application/json
      parameters:
        - name: site
          required: true
          type: string
          in: query
          description: site name

        - name: userlist
          in: body
          description: list of users
          required: true
          schema:
            "$ref": "#/definitions/Userlist"


      operationId: trackingRevisit

      responses:
        200:
          description:  return list of users which need revisit
          schema:
            type: string

1 个答案:

答案 0 :(得分:0)

如果您移动消耗并生成最高级别,它将开始工作

使其成为json消费者的另一种方法。这是go-swagger中的一个错误。

您可以观看此问题:https://github.com/go-swagger/go-swagger/issues/987以获得正确的解决方案