RESTXQ advice?

Thursday, 19 January 2023 · 5 messages
HR
Hans-Jürgen Rennau19 Jan 2023 15:20 #
Hello, I encounter a problem with a RESTXQ application, perhaps someone can help me? *Goal:* Download a file with an encoding dependent on a user entry. The request contains *form parameters*. *Attempt:* I can control the encoding using the %output annotation. However, this annotation must be a literal string, I think. Therefore I tried *web:forward*, as well as *web:redirect*, thus triggering the execution of a function with the desired %output declaration. *Issue:* the form parameters get lost. How to achieve the goal? Thank you for your advice. With kind regards, Hans-Jürgen
declare %output:method("text")
        %output:encoding("utf-8")
        %rest:path("*/util/download/text*")
        %rest:form-param("text", "{$text}", "")
        %rest:form-param("file-name", "{$fileName}", "text.txt")
        %rest:form-param("encoding", "{$encoding}", "utf-8")
        function *f:downloadText*(
            $text as xs:string,
            $fileName as xs:string,
            $encoding as xs:string) {
  if ($encoding eq 'cp1252') then *web:forward*('
*/util/download/text-cp1252*')
  else ...

declare %output:method("text")
        %output:encoding("cp1252")
        %rest:path("*/util/download/text-cp1252*")
        %rest:form-param("text", "{$text}", "")
        %rest:form-param("file-name", "{$fileName}", "text.txt")
        function *f:downloadTextCp1252*(
            $text as xs:string,
            $fileName as xs:string) {
  let $text := replace($text, '
', '
')
  return (
  <rest:response>
    <http:response status="200">
      <http:header name="Content-Disposition" value='attachment;
filename="{$fileName}"'/>
      <http:header name="Content-Language" value="en"/>
      <http:header name="Content-Type" value="text/csv; charset=cp1252"/>
    </http:response>
  </rest:response>,
  $text
  )
};
PS: The <http:header> seems to be without effect - experiments seemd to show that I have to use the %output declaration.
IL
Imsieke, Gerrit, le-tex19 Jan 2023 15:28 #
Hi Hans-Jürgen, I had a similar problem 2 weeks ago where I needed to be flexible with a public identifier in serialization, and I switched from %output:doctype-public to serialize(…, map{'doctype-public': if ($niso-version = '1.2') then $tr:niso-1-2-public-identifier else $tr:niso-1-0-public-identifier, 'doctype-system': 'NISO-STS-interchange-1-mathml3.dtd', 'indent': 'no'} ) as the function’s output. Maybe you can also use serialize() inside your function? Gerrit
Quoted text · Hans-Jürgen Rennau · 60 lines
On 19.01.2023 15:20, Hans-Jürgen Rennau wrote: > Hello, > > I encounter a problem with a RESTXQ application, perhaps someone can > help me? > > *Goal:* Download a file with an encoding dependent on a user entry. The > request contains /form parameters/. > > *Attempt:* > I can control the encoding using the %output annotation. However, this > annotation must be a literal string, I think. Therefore I tried > /web:forward/, as well as /web:redirect/, thus triggering the execution > of a function with the desired %output declaration. > > *Issue:* the form parameters get lost. > > How to achieve the goal? Thank you for your advice. > > With kind regards, > Hans-Jürgen > > declare %output:method("text") > %output:encoding("utf-8") > %rest:path("*/util/download/text*") > %rest:form-param("text", "{$text}", "") > %rest:form-param("file-name", "{$fileName}", "text.txt") > %rest:form-param("encoding", "{$encoding}", "utf-8") > function *f:downloadText*( > $text as xs:string, > $fileName as xs:string, > $encoding as xs:string) { > if ($encoding eq 'cp1252') then > *web:forward*('*/util/download/text-cp1252*') > else ... > declare %output:method("text") > %output:encoding("cp1252") > %rest:path("*/util/download/text-cp1252*") > %rest:form-param("text", "{$text}", "") > %rest:form-param("file-name", "{$fileName}", "text.txt") > function *f:downloadTextCp1252*( > $text as xs:string, > $fileName as xs:string) { > let $text := replace($text, '&#xD;&#xA;', '&#xA;') > return ( > <rest:response> > <http:response status="200"> > <http:header name="Content-Disposition" value='attachment; > filename="{$fileName}"'/> > <http:header name="Content-Language" value="en"/> > <http:header name="Content-Type" value="text/csv; charset=cp1252"/> > </http:response> > </rest:response>, > $text > ) > }; > > PS: The <http:header> seems to be without effect - experiments seemd to > show that I have to use the %output declaration. >
-- Gerrit Imsieke Geschäftsführer / Managing Director le-tex publishing services GmbH Weissenfelser Str. 84, 04229 Leipzig, Germany Phone +49 341 355356 110, Fax +49 341 355356 510 gerrit.imsieke at le-tex.de, http://www.le-tex.de Registergericht / Commercial Register: Amtsgericht Leipzig Registernummer / Registration Number: HRB 24930 Geschäftsführer / Managing Directors: Gerrit Imsieke, Svea Jelonek, Thomas Schmidt
HR
Hans-Juergen Rennau19 Jan 2023 15:45 #
Thank you, Gerrit! I am not sure, though, that the remedy can be adapted to my case. The encoding is a level deeper, so-to-speak, than the text of a document type declaration. In your case, the solution is a manipulation of the contents of a string, but the encoding is a different matter. I mean, this would amount to manipulating the bytes, rather than the characters, which to deliver in the response! Is this not simply out of scope of what a query may attempt to do? Frankly, it is beyond what I can imagine. But I take note of your idea and will try to pursue it further if no other solutions emerge. Kind regards,Hans-Jürgen Am Donnerstag, 19. Januar 2023 um 15:28:57 MEZ hat Imsieke, Gerrit, le-tex <gerrit.imsieke at le-tex.de> Folgendes geschrieben: Hi Hans-Jürgen, I had a similar problem 2 weeks ago where I needed to be flexible with a public identifier in serialization, and I switched from %output:doctype-public to serialize(…, map{'doctype-public': if ($niso-version = '1.2') then $tr:niso-1-2-public-identifier else $tr:niso-1-0-public-identifier, 'doctype-system': 'NISO-STS-interchange-1-mathml3.dtd', 'indent': 'no'} ) as the function’s output. Maybe you can also use serialize() inside your function? Gerrit
Quoted text · Hans-Jürgen Rennau · 60 lines
On 19.01.2023 15:20, Hans-Jürgen Rennau wrote: > Hello, > > I encounter a problem with a RESTXQ application, perhaps someone can > help me? > > *Goal:* Download a file with an encoding dependent on a user entry. The > request contains /form parameters/. > > *Attempt:* > I can control the encoding using the %output annotation. However, this > annotation must be a literal string, I think. Therefore I tried > /web:forward/, as well as /web:redirect/, thus triggering the execution > of a function with the desired %output declaration. > > *Issue:* the form parameters get lost. > > How to achieve the goal? Thank you for your advice. > > With kind regards, > Hans-Jürgen > > declare %output:method("text") > %output:encoding("utf-8") > %rest:path("*/util/download/text*") > %rest:form-param("text", "{$text}", "") > %rest:form-param("file-name", "{$fileName}", "text.txt") > %rest:form-param("encoding", "{$encoding}", "utf-8") > function *f:downloadText*( > $text as xs:string, > $fileName as xs:string, > $encoding as xs:string) { > if ($encoding eq 'cp1252') then > *web:forward*('*/util/download/text-cp1252*') > else ... > declare %output:method("text") > %output:encoding("cp1252") > %rest:path("*/util/download/text-cp1252*") > %rest:form-param("text", "{$text}", "") > %rest:form-param("file-name", "{$fileName}", "text.txt") > function *f:downloadTextCp1252*( > $text as xs:string, > $fileName as xs:string) { > let $text := replace($text, '&#xD;&#xA;', '&#xA;') > return ( > <rest:response> > <http:response status="200"> > <http:header name="Content-Disposition" value='attachment; > filename="{$fileName}"'/> > <http:header name="Content-Language" value="en"/> > <http:header name="Content-Type" value="text/csv; charset=cp1252"/> > </http:response> > </rest:response>, > $text > ) > }; > > PS: The <http:header> seems to be without effect - experiments seemd to > show that I have to use the %output declaration. >
-- Gerrit Imsieke Geschäftsführer / Managing Director le-tex publishing services GmbH Weissenfelser Str. 84, 04229 Leipzig, Germany Phone +49 341 355356 110, Fax +49 341 355356 510 gerrit.imsieke at le-tex.de, http://www.le-tex.de Registergericht / Commercial Register: Amtsgericht Leipzig Registernummer / Registration Number: HRB 24930 Geschäftsführer / Managing Directors: Gerrit Imsieke, Svea Jelonek, Thomas Schmidt
CG
Christian Grünin reply to Hans-Jürgen Rennau, 19 Jan 2023, 15:2019 Jan 2023 16:18 #
Hi Hans-Jürgen, Here’s a minimized example that demonstrates how to set a custom text encoding for your HTTP response: declare %rest:path('convert') %rest:query-param('encoding', '{$encoding}') %output:method('text') function local:convert($encoding) { web:response-header(map { 'encoding': $encoding }), 'äöü' }; The function can e.g. be called with:
curl -v "http://localhost:8080/convert?encoding=CP1252"
curl -v "http://localhost:8080/convert?encoding=UTF8"
...
web:response-header is just a convenience function for creating HTTP response headers [1]. Note that the original result may implicitly be “fixed” if the function is called from the browser, an HTTP library or from within a JavaScript framework. Hope this helps, Christian [1] https://docs.basex.org/wiki/Web_Module#web:response-header
Quoted text · Hans-Jürgen Rennau · 54 lines
On Thu, Jan 19, 2023 at 3:20 PM Hans-Jürgen Rennau <hjrennau at gmail.com> wrote: > > Hello, > > I encounter a problem with a RESTXQ application, perhaps someone can help me? > > Goal: Download a file with an encoding dependent on a user entry. The request contains form parameters. > > Attempt: > I can control the encoding using the %output annotation. However, this annotation must be a literal string, I think. Therefore I tried web:forward, as well as web:redirect, thus triggering the execution of a function with the desired %output declaration. > > Issue: the form parameters get lost. > > How to achieve the goal? Thank you for your advice. > > With kind regards, > Hans-Jürgen > > declare %output:method("text") > %output:encoding("utf-8") > %rest:path("/util/download/text") > %rest:form-param("text", "{$text}", "") > %rest:form-param("file-name", "{$fileName}", "text.txt") > %rest:form-param("encoding", "{$encoding}", "utf-8") > function f:downloadText( > $text as xs:string, > $fileName as xs:string, > $encoding as xs:string) { > if ($encoding eq 'cp1252') then web:forward('/util/download/text-cp1252') > else ... > > declare %output:method("text") > %output:encoding("cp1252") > %rest:path("/util/download/text-cp1252") > %rest:form-param("text", "{$text}", "") > %rest:form-param("file-name", "{$fileName}", "text.txt") > function f:downloadTextCp1252( > $text as xs:string, > $fileName as xs:string) { > let $text := replace($text, '&#xD;&#xA;', '&#xA;') > return ( > <rest:response> > <http:response status="200"> > <http:header name="Content-Disposition" value='attachment; filename="{$fileName}"'/> > <http:header name="Content-Language" value="en"/> > <http:header name="Content-Type" value="text/csv; charset=cp1252"/> > </http:response> > </rest:response>, > $text > ) > }; > > PS: The <http:header> seems to be without effect - experiments seemd to show that I have to use the %output declaration. >
HR
Hans-Juergen Rennau19 Jan 2023 17:04 #
Most helpful, cordial thanks, Christian! Kind regards,Hans-Jürgen Am Donnerstag, 19. Januar 2023 um 16:19:08 MEZ hat Christian Grün <christian.gruen at gmail.com> Folgendes geschrieben: Hi Hans-Jürgen, Here’s a minimized example that demonstrates how to set a custom text encoding for your HTTP response: declare %rest:path('convert') %rest:query-param('encoding', '{$encoding}') %output:method('text') function local:convert($encoding) { web:response-header(map { 'encoding': $encoding }), 'äöü' }; The function can e.g. be called with:
curl -v "http://localhost:8080/convert?encoding=CP1252"
curl -v "http://localhost:8080/convert?encoding=UTF8"
...
web:response-header is just a convenience function for creating HTTP response headers [1]. Note that the original result may implicitly be “fixed” if the function is called from the browser, an HTTP library or from within a JavaScript framework. Hope this helps, Christian [1] https://docs.basex.org/wiki/Web_Module#web:response-header
Quoted text · Hans-Jürgen Rennau · 54 lines
On Thu, Jan 19, 2023 at 3:20 PM Hans-Jürgen Rennau <hjrennau at gmail.com> wrote: > > Hello, > > I encounter a problem with a RESTXQ application, perhaps someone can help me? > > Goal: Download a file with an encoding dependent on a user entry. The request contains form parameters. > > Attempt: > I can control the encoding using the %output annotation. However, this annotation must be a literal string, I think. Therefore I tried web:forward, as well as web:redirect, thus triggering the execution of a function with the desired %output declaration. > > Issue: the form parameters get lost. > > How to achieve the goal? Thank you for your advice. > > With kind regards, > Hans-Jürgen > > declare %output:method("text") > %output:encoding("utf-8") > %rest:path("/util/download/text") > %rest:form-param("text", "{$text}", "") > %rest:form-param("file-name", "{$fileName}", "text.txt") > %rest:form-param("encoding", "{$encoding}", "utf-8") > function f:downloadText( > $text as xs:string, > $fileName as xs:string, > $encoding as xs:string) { > if ($encoding eq 'cp1252') then web:forward('/util/download/text-cp1252') > else ... > > declare %output:method("text") > %output:encoding("cp1252") > %rest:path("/util/download/text-cp1252") > %rest:form-param("text", "{$text}", "") > %rest:form-param("file-name", "{$fileName}", "text.txt") > function f:downloadTextCp1252( > $text as xs:string, > $fileName as xs:string) { > let $text := replace($text, '&#xD;&#xA;', '&#xA;') > return ( > <rest:response> > <http:response status="200"> > <http:header name="Content-Disposition" value='attachment; filename="{$fileName}"'/> > <http:header name="Content-Language" value="en"/> > <http:header name="Content-Type" value="text/csv; charset=cp1252"/> > </http:response> > </rest:response>, > $text > ) > }; > > PS: The <http:header> seems to be without effect - experiments seemd to show that I have to use the %output declaration. >

⚡Generated with XQuery