drakma icon indicating copy to clipboard operation
drakma copied to clipboard

Not able to send different content-types on multipart/form-data requests

Open zmyrgel opened this issue 1 year ago • 0 comments

I have API end point which expects POST requests in multipart/form-data containing input as application/json and file upload. AFAIK drakma does not allow such combination and only supports sending text/plain strings with file uploads.

This is sample curl request accepted by the API:

;; curl -H "authorization: Bearer $TOKEN" -X POST -F "metadata={}" -F "typeId=10" \
;; -F "file=@/home/user/Downloads/empty_green.pdf;filename=empty_green.pdf" \
;; https://my-api.com/api/v1/versions

I've tried the API call with following drakma call:

(let* ((file "/home/user/Downloads/empty_green.pdf")
                   (filepath (uiop:ensure-pathname file))
                   (mime-type (trivial-mimes:mime-probe filepath))
                   (params `(("typeId" . 10)
                             ("metadata" . "{}")
                             (|file| . (,filepath :content-type ,mime-type :filename "empty_green.pdf")))))
              (drakma:http-request (format nil "~a/versions" *api-root*)
                                   :method :post
                                   :form-data t
                                   :parameters params
                                   :content-type "application/json"
                                   :additional-headers `((Authorization . ,(format nil "Bearer ~a" *token*)))))

The above results in error:

Don't know what to do with name/value pair ("typeId" . 10) in multipart/form-data body.

Looking at the drakma source it seems to assume all data for multipart/form-data should be strings so it does not handle the number sending above. Adding quotes around typeId value makes drakma complete the request but the API rejects the request with error that typeId must be number and metadata must be object.

Am I doing something wrong or could drakma be extended to cover such API use?

zmyrgel avatar Sep 10 '23 12:09 zmyrgel