如何使用go-swagger编写一个swagger端点,其中可以延迟对请求体的消费?

huangapple go评论84阅读模式
英文:

How do I write a swagger endpoint using go-swagger where I can delay the consumption of the body in its parameters?

问题

我需要定义一个 Swagger 端点,该端点需要接受文本和 ZIP 格式的媒体类型。在生成的包中,是否可以直接传递 io.ReadCloser 而不是将其重新分配到不同的类型(如字符串)中?

英文:

I need to define a swagger endpoint that needs to take in media types text and zip. Instead of having the generated package consume it and reassign it into separate types such as string, can I get the io.Readcloser passed through directly?

答案1

得分: 1

Swagger规范:

"parameters": [
  {
    "name": "foo",
    "in": "body",
    "schema": {
      "type": "string",
      "format": "binary"
    }
  }
]

生成的参数:

type SomeParams struct {
    Foo io.ReadCloser
}

在生成的BindRequest方法中,请求体的ReadCloser被赋值给Foo字段:

    if runtime.HasBody(r) {
        o.Foo = r.Body
    }
英文:

Swagger spec:

"parameters": [
  {
    "name": "foo",
    "in": "body",
    "schema": {
      "type": string,
      "format": "binary"
    }
  }
]

Generated parameter:

type SomeParams struct {
    Foo io.ReadCloser
}

And in the generated BindRequest method, the request body ReadCloser is assigned to the Foo field:

    if runtime.HasBody(r) {
        o.Foo = r.Body
    }

huangapple
  • 本文由 发表于 2022年2月8日 06:05:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/71026017.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定