Forum Discussion

andreiclara's avatar
andreiclara
New Contributor
3 years ago

Swagger example for multipart/form-data sending file in body

I am trying to add an example in "examples.json" external file a swagger.yaml. Until now for PUT/POST I have:

{
"path": "/api/books/{id}",
"method": "PUT",
"examples": [
{
"name": "Update book with file.",
"url": "/api/book/8b7hhhd9-a21b-3m2l-k384-ck88d8b84h51",
"description": "Updates book with ID 8b7dbbb0-a43f-4c6c-a339-cf11b8b04f72 with provided file.",
"body": "-----12345W\r\nContent-Disposition=form-data; name=\"data\"\r\n\r\nUUHHGT5Y\r\n-----12345\r\n",
"headers": {
"Content-Type": "multipart/form-data; boundary=-----12345"
}
}
]
}

 

and even if I remove "body" parameter or change it, all I get is this error:

 

have following errors - parameter 'body': 'multipart: NextPart: EOF'
  •       examples: 
            foo:
              summary: A foo example
              value: {"foo": "bar"}
            bar:
              summary: A bar example
              value: {"bar": "baz"}
        'application/xml':
          examples: 
            xmlExample:
              summary: This is an example in XML
              externalValue: 'http://example.org/examples/address-example.xml'
    • andreiclara's avatar
      andreiclara
      New Contributor

      Thank you for your answer, but I don't understand where I can add this example. I was trying to add example in external file "examples.json". The code above should be added in "examples.json" or in swagger.yaml? And what section should I add it? In the responses or schema examples?

      Thank you

      • chichepo's avatar
        chichepo
        Frequent Contributor

        Hi andreiclara 

        the examples are defined under the components clause as shown here 

        look here for further examples.

         

        Another solution is to save your examples collection within a domain.
        In such case, your Domain is defining all your "reusable" definitions, under the components clause.

        For example:

        In the API file:

         

        In the Domain file:

         

        Let us know if it is enough clear