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

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

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规范:

  1. "parameters": [
  2. {
  3. "name": "foo",
  4. "in": "body",
  5. "schema": {
  6. "type": "string",
  7. "format": "binary"
  8. }
  9. }
  10. ]

生成的参数:

  1. type SomeParams struct {
  2. Foo io.ReadCloser
  3. }

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

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

Swagger spec:

  1. "parameters": [
  2. {
  3. "name": "foo",
  4. "in": "body",
  5. "schema": {
  6. "type": string,
  7. "format": "binary"
  8. }
  9. }
  10. ]

Generated parameter:

  1. type SomeParams struct {
  2. Foo io.ReadCloser
  3. }

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

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

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:

确定