【问题标题】:Why can't I upload config file in the POST method using Postman?为什么我不能使用 Postman 在 POST 方法中上传配置文件?
【发布时间】:2021-07-30 10:37:44
【问题描述】:

您好,我一直在努力解决我的问题,以至于不得不请教其他人。

我正在使用一些 API,我想通过 Postman 使用 POST 方法创建资源。要创建该资源,我必须将 kubeconfig 文件附加到 POST 请求。这个资源需要有名称、描述,当然还有配置文件。所以我填写了必填字段:

POSTMAN fields

但我收到响应“错误请求”:

Invalid Input:: Does not match pattern '^([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]$'

看起来我不明白如何以这种方式上传文件,因为我的请求正文看起来像:

Request Body
    metadata: "{"name": "edge01", "description": "Edge k8s cluster"}"
    file: undefine

在 API 文档中,我们可以发现请求正文应该是这样的:

Request body                                                     multipart/form-data

metadata
    object

file
    string($binary)

下面的整个 POST 条目:

Network
Request Headers
    Accept: */*
    Postman-Token: 8857f718-bb05-404c-865a-51ee3c0abe6f
    Host: <Hidden_IP>:30461
    Accept-Encoding: gzip, deflate, br
    Connection: keep-alive
    Content-Type: multipart/form-data; boundary=--------------------------175308278007362265143565
    Content-Length: 3518
Request Body
    metadata: "{"name": "edge01", "description": "Edge k8s cluster"}"
    file: undefined
Response Headers
    Content-Type: text/plain; charset=utf-8
    X-Content-Type-Options: nosniff
    Date: Fri, 30 Jul 2021 10:19:58 GMT
    Content-Length: 84
Response Body
    Invalid Input:: Does not match pattern '^([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]$'↵

我找不到有关上述响应的任何信息。

如果有人需要更多信息,请发表评论。这个字符串($binary) 很有趣,但我不明白它的含义,并且在 POSTMAN 上下文中找不到任何信息。

这是一段 API yaml:

  /cluster-providers/{cluster-providers-name}/clusters:
parameters:
  - $ref: '#/components/parameters/clusterProviderName'
post:
  tags:
    - Clusters
  summary: Add Cluster
  description: Add a new `cluster`
  operationId: addClusterToClusterProvider
  responses:
    '201':
      description: Created
      content:
        application/json: # operation response mime type
          schema:
            $ref: '#/components/schemas/Metadata'
    '400':
      description: Bad Request
    '404':
      description: Not Found
    '409':
      description: Conflict
    '422':
      description: Unprocessable Entity
    '500':
      description: Internal Server Error
  requestBody:
    content:
      multipart/form-data: # Media type
        schema:            # Request payload
          $ref: '#/components/schemas/AppData'

【问题讨论】:

    标签: postman http-post multipartform-data


    【解决方案1】:

    metadata 添加到raw 下的正文中,而不是form-data。这应该是一个对象,像这样:

    {
        "metadata": {
            "name": "edge01",
            "description": "Edge k8s cluster"
        }
    }
    

    【讨论】:

    • 您的建议不正确,因为我必须使用form-data 来传输文件。尽管如此,它真的很有帮助!我使用这个object 作为键metadata 的值并且它起作用了。对我来说,这真的是一种奇怪的语法,但你一生都在学习。感谢您的帮助。
    猜你喜欢
    • 2018-02-27
    • 1970-01-01
    • 2020-03-13
    • 1970-01-01
    • 2011-07-09
    • 2019-06-17
    • 2021-08-02
    • 2012-02-23
    • 2016-06-09
    相关资源
    最近更新 更多