RESTXQ Textarea request paramater
Friday, 7 February 2025 · 5 messages · last reply 27 Feb 2025
B
brian7 Feb 2025 18:51 #
Everyone,
I’m trying to build a comment form using RESTXQ that has a texarea element. I was hoping to use the new lines from the textarea for simple formatting but when the form is submitted the request parameter is striped of all the linefeeds ( it looks like normalized text). Is this the expected behavior?
Thanks — Brian
CG
Christian Grün22 Feb 2025 11:33 #
Hi Brian,
Some belated response: Could you show us your code?
With the following RESTXQ code, all newlines should be preserved:
declare
%rest:path('in')
%output:method('html')
function local:get() {
<form action='out' method='post'>
<textarea name='text'>A{ char('\n') }B</textarea>
<input type='submit'/>
</form>
};declare
%rest:POST
%rest:path('out')
%rest:form-param('text', '{$text}')
%output:method('html')
function local:out($text) {
<html>Codepoints: { string-to-codepoints($text) }</html>
};
Best,
Christian
Quoted text · PM · 12 lines
On Fri, Feb 7, 2025 at 6:51 PM <brian at kennison.name> wrote: > Everyone, > > I’m trying to build a comment form using RESTXQ that has a texarea > element. I was hoping to use the new lines from the textarea for simple > formatting but when the form is submitted the request parameter is striped > of all the linefeeds ( it looks like normalized text). Is this the expected > behavior? > > Thanks — Brian > >
B
brian27 Feb 2025 01:07 #
Quoted text · Christian Grün · 10 lines
> On Feb 22, 2025, at 5:33 AM, Christian Grün <christian.gruen at gmail.com> wrote: > > declare > %rest:POST > %rest:path('out') > %rest:form-param('text', '{$text}') > %output:method('html') > function local:out($text) { > <html>Codepoints: { string-to-codepoints($text) }</html> > };
Indeed if you convert the text string to codepoints and the codepoints back to a string the newlines are present. I’m not sure understand why they don’t show up otherwise.
My solution was to use javascript to insert <br /> tags into the form data before it gets submitted but your method doesn’t require any javascript.
Thanks for your reply —Brian
—— Code -----
xquery version "3.1";
(: echo-post.xq: Return all data from an HTTP post to the caller. :)
module namespace comments = "https://kennison.name/comments";
declare
%rest:path("/printenv")
%rest:GET
%rest:POST
%output:method("html")
%output:media-type("text/html")
function comments:echo-post(){
let $method := request:method()
let $scheme := request:scheme()
let $host := request:hostname()
let $port := request:port()
let $path := request:path()
let $query := request:query()
let $uri := request:uri()
let $context := request:context-path()
let $address := request:address()
let $remote-host := request:remote-hostname()
let $remote-address := request:remote-address()
let $remote-port := request:remote-port()
return
<html>
<head>
<title>Print Environ</title>
<link rel="stylesheet" href="http://localhost/assets/css/jdoe.css"/>
<link rel="stylesheet" href="http://localhost/assets/css/jdoe2.css"/>
<style>
<![CDATA[
th, td {
border: 1px solid black;
padding: 8px;
margin-left:10px;
}
thead th {
width: 25%;
}]]>
</style>
</head>
<body>
<section id="home">
<article>
<h1>Echo Request</h1>
<p> These are the variables and parmaters available through the request.</p>
<h3>Request Variables</h3>
<table>
<thead>
<tr><th>Variable</th><th>Value</th></tr>
</thead>
<tbody>
<tr><td>Method:</td><td>{$method}</td></tr>
<tr><td>Scheme:</td><td>{$scheme}</td></tr>
<tr><td>Host:</td><td>{$host}</td></tr>
<tr><td>Port:</td><td>{$port}</td></tr>
<tr><td>Path:</td><td>{$path}</td></tr>
<tr><td>Query:</td><td>{$query}</td></tr>
<tr><td>URI:</td><td>{$uri}</td></tr>
<tr><td>Context:</td><td>{$context}</td></tr>
<tr><td>Remote Host:</td><td>{$remote-host}</td></tr>
<tr><td>Remote Addres:</td><td>{$remote-address}</td></tr>
</tbody>
</table>
<h3>Request Parameters</h3>
<table>
<thead>
<tr><th>Param</th><th>Value</th></tr>
</thead>
<tbody>
{
for $p in request:parameter-names()
return
<tr><td>{$p}</td><td>{request:parameter($p)}</td></tr>
}
</tbody>
</table>
<h3>Request Headers</h3>
<table>
<thead>
<tr><th>Header</th><th>Value</th></tr>
</thead>
<tbody>
{
for $h in request:header-names()
return
<tr><td>{$h}</td><td>{request:header($h)}</td></tr>
}
</tbody>
</table>
<h3>Request Cookies</h3>
<table>
<thead>
<tr><th>Cookie</th><th>Value</th></tr>
</thead>
<tbody>
{
for $c in request:cookie-names()
return
<tr><td>{$c}</td><td>{request:cookie($c)}</td></tr>
}
</tbody>
</table>
<h3>Request Attributes</h3>
<table>
<thead>
<tr><th>Attribute</th><th>Value</th></tr>
</thead>
<tbody>
{
for $a in request:attribute-names()
return
<tr><td>{$a}</td><td>{request:attribute($a)}</td></tr>
}
</tbody>
</table>
</article>
</section>
</body>
</html>
};—— Comment Form -----
Name:
Email:
Comment:
—— Printed Result ——
—— as you can see the newlines don’t show in results
Echo Request
These are the variables and parmaters available through the request.
Request Variables
Variable Value
Method: POST
Scheme: http
Host: 127.0.0.1
Port: 8080
Path: /printenv
Query:
URI: http://127.0.0.1:8080/printenv
Context:
Remote Host: 127.0.0.1
Remote Addres: 127.0.0.1
Request Parameters
Param Value
uri ripple
name kdkdk
email dkdk@kdkdk
comment One Two three
Request Headers
Header Value
Origin http://localhost
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Priority u=0, i
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.3 Safari/605.1.15 Referer http://localhost/dev-pages/comments/comment-form.html X-Forwarded-Host localhost
Connection keep-alive
Sec-Fetch-Site same-origin
Sec-Fetch-Dest document
Host 127.0.0.1:8080
Accept-Encoding gzip, deflate
Sec-Fetch-Mode navigate
Upgrade-Insecure-Requests 1
X-Forwarded-For ::1
Accept-Language en-US,en;q=0.9
Content-Length 84
X-Forwarded-Server MB-15.local
Content-Type application/x-www-form-urlencoded
Request Cookies
Cookie Value
Request Attributes
Attribute Value
CG
Christian Grün27 Feb 2025 07:33 #
Quoted text · brian · 5 lines
> > Indeed if you convert the text string to codepoints and the codepoints > back to a string the newlines are present. I’m not sure understand why they > don’t show up otherwise. >
It’s the HTML rendering that suppresses newlines. You can add them server-side…
let $value := request:parameter($p)
return <tr><td>{
for $line in tokenize($value, char('\n'))
return ($line, <br/>)
}</td><td>…or return the result inside a pre tag:
<td><pre>{
request:parameter($p)
}</pre></td>BK
Brian Kennison27 Feb 2025 13:22 #
Quoted text · Christian Grün · 4 lines
> On Feb 27, 2025, at 1:33 AM, Christian Grün <christian.gruen at gmail.com> wrote: > > It’s the HTML rendering that suppresses newlines. You can add them server-side… >
Christian thank you for your help! —Brian
Written for BaseX 11.7. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery