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
On Thu, Jan 19, 2023 at 3:20 PM Hans-Jürgen Rennau hjrennau@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, '
', '
') 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.