jbrehr
jbrehr

Reputation: 815

eXist-db how to add file to multipart http request (expath http request)

I am trying to send a post request in eXist-db 6.2 using the expath http client to upload a file from my db. The provide requires multipart. However the provider is responding to the request below with 500 and the specific message Unable to retrieve file from request. I'm not able to identify why the file can't be retrieved. Is there a problem with my <body> attributes or how I'm including the xml document?

import module namespace http="http://expath.org/ns/http-client";

let $xml-doc-to-send := doc("/db/apps/myapp/data/myfile.xml")

let $request := 
    http:send-request(
       <http:request http-version="1.1" method="post">
            <http:header name="X-API-KEY" value="-----------"/>
            <http:header name="accept" value="application/json"/>
            <http:header name="Content-Type" value="multipart/form-data"/>
            <http:multipart media-type="multipart/form-data" boundary="_bd8e2bb8-ba83-42e0-8e2b-b8ba83a2e042_">
                <http:header name="Content-Disposition" value='form-data; name="file";filename="myfile.xml";type="text/xml"'/>
                <http:body media-type="text/xml" method="xml"/>
            </http:multipart>
       </http:request>,
       "https://some.provider/api/uploads",
       $xml-doc-to-send
   )

return
    $request

Many thanks in advance for any assistance.

Upvotes: 0

Views: 44

Answers (1)

adamretter
adamretter

Reputation: 3517

How about this:

let $xml := <http:request xmlns:http="http://expath.org/ns/http-client" http-version="1.1" method="post">
            <http:header name="X-API-KEY" value="-------------"/>
            <http:header name="accept" value="application/json"/>
            <http:multipart media-type="multipart/form-data" boundary="_16fab392-c8e4-434d-bab3-92c8e4734dc0_">
                <http:header name="Content-Disposition" value='form-data; name="file"; filename="TC0001.xml"'/>
                <http:body media-type="text/xml"/>
            </http:multipart>
        </http:request>

return
     http:send-request($xml, "https://some.provider/api/uploads", doc("/db/apps/myapp/data/myfile.xml"))

Upvotes: 0

Related Questions