Re: [basex-talk] Forcing download of JSON doc

2019-08-01 Thread Tim Thompson
Thanks for the tip! By declaring the %output:method("json") annotation AND
calling the serialize function, I was basically reserializing the data.

--
Tim A. Thompson
Discovery Metadata Librarian
Yale University Library

On Thu, Aug 1, 2019 at 8:55 AM Marco Lettere  wrote:

> Maybe using json:serialize() rather than serialize?
>
> In general if output:method is set to Json it should be sufficient to
> return map{} or array{} or alternatively the XML version of a json document
> without having to explicitly serialize ...
>
> M.
>
> On 01/08/19 14:36, Tim Thompson wrote:
>
> Thank you, Marco. So, the download issue seems to be with XForms
> (XSLTForms) rather than BaseX. If I do a file upload from a plain HTML
> form, the file download is triggered correctly. However, even though I have
> set "application/json" as the Content-Type, the contents of the downloaded
> file are an escaped string rather than a serialized JSON object: e.g.,
>
> "{\n  \"key\": \"value\"\n}"
>
> Is there a way to produce a properly serialized JSON file for download?
>
> --
> Tim A. Thompson
> Discovery Metadata Librarian
> Yale University Library
>
> On Thu, Aug 1, 2019 at 3:03 AM Marco Lettere  wrote:
>
>> Hi Tim,
>> downloading from a browser when posting a form is usually a bit tricky.
>> For me it always ended up with the creation of an artificial 
>> element with an href crafted from the form parameters and with an attribute
>> download="filename.ext" added to it.
>> Anyway you could try with returning a "forced" application/octet-stream
>> Content-Type header 
>>
>> M.
>>
>> On 01/08/19 05:00, Tim Thompson wrote:
>>
>> Hello,
>>
>> I am posting a JSON doc to a RESTXQ function from XForms and trying to
>> set a Content-Disposition header to trigger a file download. Is this
>> possible to do without first saving the JSON doc as a file? Right now, the
>> RESTXQ function returns the correct headers, but does not force a file
>> download dialog.
>>
>> Thanks in advance!
>> Tim
>>
>> --
>> Query:
>>
>> xquery version "3.1";
>>
>> module namespace wrap = "http://example.org/wrap";;
>>
>> declare
>>   %rest:path("/wrap")
>>   %rest:POST("{$data}")
>>   %output:method("json")
>> function wrap:process(
>>   $data as node()
>> ) as item()* {
>>   let $name := data($data/data/upload/file/@name)
>>   let $json := json-to-xml($data/data/upload/file)
>>   let $header :=
>> ``[attachment; filename="`{$name}`";]``
>>   return
>> (
>> 
>>   > status="200" message="OK">
>> >   name="Content-Type"
>>   value="application/json"/>
>> >   name="Content-Disposition"
>>   value="{$header}"/>
>>   
>> ,
>>
>> http://www.w3.org/2005/xpath-functions";>
>> >   key="Profile">
>>   > key="resourceTemplates">{$json}
>>   > key="title">Wrapper_Profile
>>   > key="id">Wrapper:Profile:BF2
>>   > key="description">Profile wrapper for resource
>> templates
>>   > key="author">Wrapper
>>   > key="schema">
>> https://ld4p.github.io/sinopia/schemas/0.0.9/profile.json
>>   > key="date">{current-dateTime()}
>> 
>>  => xml-to-json() => parse-json() => serialize(map {'method':
>> 'json', 'use-character-maps' : map {'/' : '/'}})
>> )
>> };
>>
>>
>> --
>> Tim A. Thompson
>> Discovery Metadata Librarian
>> Yale University Library
>>
>>
>>
>


Re: [basex-talk] Forcing download of JSON doc

2019-08-01 Thread Marco Lettere

Maybe using json:serialize() rather than serialize?

In general if output:method is set to Json it should be sufficient to 
return map{} or array{} or alternatively the XML version of a json 
document without having to explicitly serialize ...


M.

On 01/08/19 14:36, Tim Thompson wrote:
Thank you, Marco. So, the download issue seems to be with XForms 
(XSLTForms) rather than BaseX. If I do a file upload from a plain HTML 
form, the file download is triggered correctly. However, even though I 
have set "application/json" as the Content-Type, the contents of the 
downloaded file are an escaped string rather than a serialized JSON 
object: e.g.,


"{\n  \"key\": \"value\"\n}"
Is there a way to produce a properly serialized JSON file for download?

--
Tim A. Thompson
Discovery Metadata Librarian
Yale University Library

On Thu, Aug 1, 2019 at 3:03 AM Marco Lettere > wrote:


Hi Tim,
downloading from a browser when posting a form is usually a bit
tricky. For me it always ended up with the creation of an
artificial  element with an href crafted from the form
parameters and with an attribute download="filename.ext" added to it.
Anyway you could try with returning a "forced"
application/octet-stream Content-Type header 

M.

On 01/08/19 05:00, Tim Thompson wrote:

Hello,

I am posting a JSON doc to a RESTXQ function from XForms and
trying to set a Content-Disposition header to trigger a file
download. Is this possible to do without first saving the JSON
doc as a file? Right now, the RESTXQ function returns the correct
headers, but does not force a file download dialog.

Thanks in advance!
Tim

--
Query:

xquery version "3.1";

module namespace wrap = "http://example.org/wrap";;

declare
  %rest:path("/wrap")
  %rest:POST("{$data}")
  %output:method("json")
function wrap:process(
  $data as node()
) as item()* {
  let $name := data($data/data/upload/file/@name)
  let $json := json-to-xml($data/data/upload/file)
  let $header :=
    ``[attachment; filename="`{$name}`";]``
  return
    (
    
      
        
        
      
    ,

    http://www.w3.org/2005/xpath-functions";>
        
          {$json}
          Wrapper_Profile
          Wrapper:Profile:BF2
          Profile wrapper for resource
templates
          Wrapper
          https://ld4p.github.io/sinopia/schemas/0.0.9/profile.json
          {current-dateTime()}
        
     => xml-to-json() => parse-json() => serialize(map
{'method': 'json', 'use-character-maps' : map {'/' : '/'}})
    )
};


--
Tim A. Thompson
Discovery Metadata Librarian
Yale University Library







Re: [basex-talk] Forcing download of JSON doc

2019-08-01 Thread Tim Thompson
Thank you, Marco. So, the download issue seems to be with XForms
(XSLTForms) rather than BaseX. If I do a file upload from a plain HTML
form, the file download is triggered correctly. However, even though I have
set "application/json" as the Content-Type, the contents of the downloaded
file are an escaped string rather than a serialized JSON object: e.g.,

"{\n  \"key\": \"value\"\n}"

Is there a way to produce a properly serialized JSON file for download?

--
Tim A. Thompson
Discovery Metadata Librarian
Yale University Library

On Thu, Aug 1, 2019 at 3:03 AM Marco Lettere  wrote:

> Hi Tim,
> downloading from a browser when posting a form is usually a bit tricky.
> For me it always ended up with the creation of an artificial 
> element with an href crafted from the form parameters and with an attribute
> download="filename.ext" added to it.
> Anyway you could try with returning a "forced" application/octet-stream
> Content-Type header 
>
> M.
>
> On 01/08/19 05:00, Tim Thompson wrote:
>
> Hello,
>
> I am posting a JSON doc to a RESTXQ function from XForms and trying to set
> a Content-Disposition header to trigger a file download. Is this possible
> to do without first saving the JSON doc as a file? Right now, the RESTXQ
> function returns the correct headers, but does not force a file download
> dialog.
>
> Thanks in advance!
> Tim
>
> --
> Query:
>
> xquery version "3.1";
>
> module namespace wrap = "http://example.org/wrap";;
>
> declare
>   %rest:path("/wrap")
>   %rest:POST("{$data}")
>   %output:method("json")
> function wrap:process(
>   $data as node()
> ) as item()* {
>   let $name := data($data/data/upload/file/@name)
>   let $json := json-to-xml($data/data/upload/file)
>   let $header :=
> ``[attachment; filename="`{$name}`";]``
>   return
> (
> 
>status="200" message="OK">
>name="Content-Type"
>   value="application/json"/>
>name="Content-Disposition"
>   value="{$header}"/>
>   
> ,
>
> http://www.w3.org/2005/xpath-functions";>
>key="Profile">
>key="resourceTemplates">{$json}
>key="title">Wrapper_Profile
>key="id">Wrapper:Profile:BF2
>key="description">Profile wrapper for resource
> templates
>key="author">Wrapper
>key="schema">
> https://ld4p.github.io/sinopia/schemas/0.0.9/profile.json
>key="date">{current-dateTime()}
> 
>  => xml-to-json() => parse-json() => serialize(map {'method':
> 'json', 'use-character-maps' : map {'/' : '/'}})
> )
> };
>
>
> --
> Tim A. Thompson
> Discovery Metadata Librarian
> Yale University Library
>
>
>


Re: [basex-talk] Forcing download of JSON doc

2019-08-01 Thread Marco Lettere

Hi Tim,
downloading from a browser when posting a form is usually a bit tricky. 
For me it always ended up with the creation of an artificial  
element with an href crafted from the form parameters and with an 
attribute download="filename.ext" added to it.
Anyway you could try with returning a "forced" application/octet-stream 
Content-Type header 


M.

On 01/08/19 05:00, Tim Thompson wrote:

Hello,

I am posting a JSON doc to a RESTXQ function from XForms and trying to 
set a Content-Disposition header to trigger a file download. Is this 
possible to do without first saving the JSON doc as a file? Right now, 
the RESTXQ function returns the correct headers, but does not force a 
file download dialog.


Thanks in advance!
Tim

--
Query:

xquery version "3.1";

module namespace wrap = "http://example.org/wrap";;

declare
  %rest:path("/wrap")
  %rest:POST("{$data}")
  %output:method("json")
function wrap:process(
  $data as node()
) as item()* {
  let $name := data($data/data/upload/file/@name)
  let $json := json-to-xml($data/data/upload/file)
  let $header :=
    ``[attachment; filename="`{$name}`";]``
  return
    (
    
      
        
        
      
    ,

    http://www.w3.org/2005/xpath-functions";>
        
          {$json}
          Wrapper_Profile
          Wrapper:Profile:BF2
                      key="description">Profile wrapper for resource 
templates

          Wrapper
                      
key="schema">https://ld4p.github.io/sinopia/schemas/0.0.9/profile.json

          {current-dateTime()}
        
     => xml-to-json() => parse-json() => serialize(map 
{'method': 'json', 'use-character-maps' : map {'/' : '/'}})

    )
};


--
Tim A. Thompson
Discovery Metadata Librarian
Yale University Library





[basex-talk] Forcing download of JSON doc

2019-07-31 Thread Tim Thompson
Hello,

I am posting a JSON doc to a RESTXQ function from XForms and trying to set
a Content-Disposition header to trigger a file download. Is this possible
to do without first saving the JSON doc as a file? Right now, the RESTXQ
function returns the correct headers, but does not force a file download
dialog.

Thanks in advance!
Tim

--
Query:

xquery version "3.1";

module namespace wrap = "http://example.org/wrap";;

declare
  %rest:path("/wrap")
  %rest:POST("{$data}")
  %output:method("json")
function wrap:process(
  $data as node()
) as item()* {
  let $name := data($data/data/upload/file/@name)
  let $json := json-to-xml($data/data/upload/file)
  let $header :=
``[attachment; filename="`{$name}`";]``
  return
(

  


  
,

http://www.w3.org/2005/xpath-functions";>

  {$json}
  Wrapper_Profile
  Wrapper:Profile:BF2
  Profile wrapper for resource
templates
  Wrapper
  
https://ld4p.github.io/sinopia/schemas/0.0.9/profile.json
  {current-dateTime()}

 => xml-to-json() => parse-json() => serialize(map {'method':
'json', 'use-character-maps' : map {'/' : '/'}})
)
};


--
Tim A. Thompson
Discovery Metadata Librarian
Yale University Library