Hi Danny,

This function is to be used when you have an entire HTTP entity which
is a multipart entity (A.K.A. the "HTTP body"), as a binary node.  I
believe this is the case with the HTTP client, when a server returns a
multipart response (which is quite rare actually).

In my example, the multipart entity is decoded by MarkLogic as part of
the HTTP request handling.  This is how xdmp:get-request-field() gets
its value.  In my example there is only one part, but my first attempt
used several parts, and they are actually decoded.  The following CURL
command (note the additional `-F foo=bar`):

    curl -u user:pwd --digest \
        -F foo=bar -F "file=@.../example.xml;type=text/xml" \
        http://localhost:8010/test/tools/fields

returns the following (with a more complete endpoint, handling
non-node fields and a few more things):

    <fields version="8.0-4">
       <field>
          <name>foo</name>
          <is-atomic>true</is-atomic>
          <filename type=""/>
          <content-type/>
          <value>bar</value>
       </field>
       <field>
          <name>file</name>
          <is-text>true</is-text>
          <is-binary>false</is-binary>
          <filename type="application/xml">example.xml</filename>
          <content-type>text/xml</content-type>
          <value>&lt;hello&gt;World!&lt;/hello&gt;</value>
       </field>
    </fields>

So the multipart entity is decoded.  The only trouble is that a file
part, with an extension ".xml" (associated to "application/xml" in the
internal mediatypes), and with a Content-Type of "text/xml", is
returned as a (document node containing a) text node.

I feel like I am missing something simple, but fail to pin point what...

Regards,

-- 
Florent Georges
http://fgeorges.org/
http://h2oconsulting.be/


On 26 November 2015 at 22:52, Danny Sokolsky wrote:
> I am not sure, but don't you need to decode the multipart form data?
>
> http://docs.marklogic.com/xdmp:multipart-decode
>
> -Danny
>
> ________________________________________
> From: [email protected] 
> [[email protected]] on behalf of Florent Georges 
> [[email protected]]
> Sent: Thursday, November 26, 2015 12:55 PM
> To: MarkLogic Developer Discussion
> Subject: Re: [MarkLogic Dev General] Retrieve a multipart/form-data file as 
> XML nodes
>
> Hi Geert,
>
> Exactly the same than with text/xml, unfortunately.
>
> --
> Florent Georges
> http://fgeorges.org/
> http://h2oconsulting.be/
>
>
> On 26 November 2015 at 21:42, Geert Josten wrote:
>> What happens if you mark it with Content-Type: application/xml?
>>
>> Cheers,
>> Geert
>>
>> On 11/26/15, 9:08 PM, "[email protected] on behalf
>> of Florent Georges" <[email protected] on behalf of
>> [email protected]> wrote:
>>
>>>Hi,
>>>
>>>I have a problem with a form, that I could narrow down to the
>>>following.  It is a POST of type multipart/form data, sending one file
>>>part:
>>>
>>>    ------------------------------ed0cb8f98262
>>>    Content-Disposition: form-data; name="file"; filename="example.xml"
>>>    Content-Type: text/xml
>>>
>>>    <hello>World!</hello>
>>>
>>>    ------------------------------ed0cb8f98262--
>>>
>>>When I use `xdmp:get-request-field('file')` to get te value, it is
>>>returned as a document node containing one text node.  If I change
>>>`text/plain` to `application/octet-stream`, the value is a binary
>>>node.
>>>
>>>I would have expected a document node with an element `hello`.  The
>>>following query reproduces the problem (accessing and outputing a few
>>>key values, to double-check my environment):
>>>
>>>    xquery version "1.0-ml";
>>>
>>>    import module namespace admin = "http://marklogic.com/xdmp/admin";
>>>       at "/MarkLogic/admin.xqy";
>>>
>>>    declare namespace xdmp = "http://marklogic.com/xdmp";;
>>>    declare namespace mt   = "http://marklogic.com/xdmp/mimetypes";;
>>>
>>>    declare function local:type-from-filename($name as xs:string) as
>>>xs:string*
>>>    {
>>>       let $ext   := fn:tokenize($name, '\.')[fn:last()]
>>>       let $types := admin:mimetypes-get(admin:get-configuration())
>>>       return
>>>          $types[mt:extensions/data() = $ext]/mt:name
>>>    };
>>>
>>>    <fields version="{ xdmp:version() }">
>>>    {
>>>       for $name     in xdmp:get-request-field-names()
>>>       let $value    := xdmp:get-request-field($name)
>>>       let $filename := xdmp:get-request-field-filename($name)
>>>       return
>>>          <field>
>>>             <name>{ $name }</name>
>>>             <is-text>{ $value/node() instance of text() }</is-text>
>>>             <is-binary>{ $value/node() instance of binary() }</is-binary>
>>>             <filename type="{ local:type-from-filename($filename)
>>>}">{ $filename }</filename>
>>>             <content-type>{
>>>xdmp:get-request-field-content-type($name) }</content-type>
>>>             {
>>>                if ( $value instance of binary() ) then
>>>                   <value>...binary...</value>
>>>                else
>>>                   <value>{ $value }</value>
>>>             }
>>>          </field>
>>>    }
>>>    </fields>
>>>
>>>When called with the following CURL command (just put the above query
>>>on a HTTP app server, and adapt the user, password, and endpoint
>>>below):
>>>
>>>    curl -u user:pwd --digest \
>>>        -F "file=@.../example.xml;type=text/xml" \
>>>        http://localhost:8010/test/tools/fields
>>>
>>>it returns the following:
>>>
>>>    <fields version="8.0-4">
>>>       <field>
>>>          <name>file</name>
>>>          <is-text>true</is-text>
>>>          <is-binary>false</is-binary>
>>>          <filename type="application/xml">example.xml</filename>
>>>          <content-type>text/xml</content-type>
>>>          <value>&lt;hello&gt;World!&lt;/hello&gt; </value>
>>>       </field>
>>>    </fields>
>>>
>>>
>>>When called with the following CURL command (note the different
>>>type=):
>>>
>>>    curl -u user:pwd --digest \
>>>        -F "file=@.../example.xml;type=application/octet-stream" \
>>>        http://localhost:8010/test/tools/fields
>>>
>>>it returns the following:
>>>
>>>    <fields version="8.0-4">
>>>       <field>
>>>          <name>file</name>
>>>          <is-text>false</is-text>
>>>          <is-binary>false</is-binary>
>>>          <filename type="application/xml">example.xml</filename>
>>>          <content-type>application/octet-stream</content-type>
>>>          <value>...binary...</value>
>>>       </field>
>>>    </fields>
>>>
>>>Did I miss something?  Shouldn't I get an XML document node?
>>>
>>>Posted on SO as well, at http://stackoverflow.com/questions/33946268/.
>>>
>>>Regards,
>>>
>>>--
>>>Florent Georges
>>>http://fgeorges.org/
>>>http://h2oconsulting.be/
>>>_______________________________________________
>>>General mailing list
>>>[email protected]
>>>Manage your subscription at:
>>>http://developer.marklogic.com/mailman/listinfo/general
>>
>> _______________________________________________
>> General mailing list
>> [email protected]
>> Manage your subscription at:
>> http://developer.marklogic.com/mailman/listinfo/general
> _______________________________________________
> General mailing list
> [email protected]
> Manage your subscription at:
> http://developer.marklogic.com/mailman/listinfo/general
> _______________________________________________
> General mailing list
> [email protected]
> Manage your subscription at:
> http://developer.marklogic.com/mailman/listinfo/general
_______________________________________________
General mailing list
[email protected]
Manage your subscription at: 
http://developer.marklogic.com/mailman/listinfo/general

Reply via email to