openapistack / openapi-client-axios

JavaScript client library for consuming OpenAPI-enabled APIs with axios
https://openapistack.co
MIT License
558 stars 67 forks source link

Issue with multipart/form-data requests #17

Closed mathoglu closed 4 years ago

mathoglu commented 4 years ago

Working with a image upload endpoint and noticed the generated client do not work as expected for multipart/form-data requests.

After some debugging it seems like the client creates a new FormData object and appends the files in the wrong way.

From the generated code:

localVarFormParams.append('files', files.join(COLLECTION_FORMATS.csv));

This will produce a request containing "[Object File], [Object File]..." in the body.

The correct way should be (tried it out and worked as expected) to something like

files.forEach((f: File) => {
  localVarFormParams.append('files[]', f));
})

This will produce the correct request body as also demonstrated here.

Im using the following OpenAPI definition for the endpoint:

"/upload": {
      "post": {
        "tags": [
          "Upload"
        ],
        "requestBody": {
          "description": "File upload",
          "required": true,
          "content": {
            "multipart/form-data": {
              "schema": {
                "type": "object",
                "properties": {
                  "files": {
                    "type": "array",
                    "items": {
                      "type": "string",
                      "format": "binary"
                    }
                  }
                }
              }
            }
          }
        },
        "responses": {...}
      }
    }
  }
anttiviljami commented 4 years ago

I don't believe openapi-client-axios has any special handling for multipart/form-data. Your problem must lie elsewhere. I'm not at all sure what you're talking about when you say "generated code".

You should just use the underlying axios client to provide the corrext request body.