swagger go中可运行的POST的示例代码

What am I doing wrong in this approach?

I try to reimplent an old API, however the go implementation. Basically i try to POST a list of strings to the server. but all i get is an error 500 from the server, whenn i try:

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"
}

This is the code i try to implement.

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

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

I use

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

to generate the server from this 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

if you move the consumes and produces up to the top level it will start to work

The other way to make it work it to set up the json consumer. This is a bug in go-swagger.

You can watch this issue: https://github.com/go-swagger/go-swagger/issues/987 for a proper resolution