How to process a POST request in SWI-Prolog? - html

I have an HTML form like this one:
<form action="test" method="post">
<input name="first_name" type="text"/>
<input name="last_name" type="text" />
<input name="age" type="text" />
<input type="submit" value="Send"/>
</form>
How do I get the values of the input fields and print them on screen, just like in any other procedural programming language such as PHP, ASP or JSP?
I tried to solve the problem the following way:
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- http_handler(root(test), reply, []).
:- http_handler('test', reply, []).
server(Port) :-
http_server(http_dispatch, [port(Port)]).
reply(Request) :-
member(method(post), Request), !,
http_read_data(Request, Data, []),
format('application/x-www-form-urlencoded', []),
format(Data).
That brought me nothing more than an error with the 500 code (internal server error).

You should use the http/http_client library (:- use_module(library(http/http_client))).
Additionally, I'm not sure how having two handlers for test will work.
Finally, I think that format(Data) might not work, especially since it is expected to return an html document.
By the way, to retrieve the values of the fields you can do something like:
http_read_data(Request, [first_name=FN, last_name=LN, age=A|_], []).
I'm pretty new with the http prolog lib, I would suggest checking http://www.pathwayslms.com/swipltuts/html/

Essentially, you'll handle the request like normal, checking that the method(Method) term in the request is method(post).
http_read_data will read the request body.
the body will be encoded like an URI query string, so uri_query_components/2
will convert it to a list of Key=Value terms
?- uri_query_components('a=b&c=d%2Bw&n=VU%20Amsterdam', Q).
Q = [a=b, c='d+w', n='VU Amsterdam'].
For others looking for similar info - if your response is json, you can use read_json_dict to get the data as a dict.

I use library(http/http_parameters). With that, I can do
load_graph(Request) :-
http_parameters(Request,
[path(Path, [atom]),
aperture(Aperture, [integer])]),
where load_graph is the handler for the form
...
html(form([action(Ref)],
dl([dt('Root Path'), dd(input([name=path, type=text, value=Default])),
dt('Aperture'), dd(select([name=aperture], Aplist)),
dt('Go!'), dd(input([type=submit, value='Load!']))
]))).

Related

How do I POST an array to the server?

I am trying to post an array of multiple geolocation addresses (generated by Google maps) to my server. The array displays correctly as an array while on the client side, however, the array arrives at the server as a string and not as an array. I am not entirely sure why this happens, also I can't figure out how to convert this string back into an array on the server.
Find below the code of the array while on the client side via the browser:
console.log("Geolocations: " ,selecedAddresses);
This codeabove yields an array of 3 items as illustrated below:
Geolocations: (3) ['Kileleshwa, Nairobi, Kenya', 'Ntinda, Kampala, Uganda', 'Uhuru Highway, Nairobi, Kenya']
When the same array is expanded for better readability:
0: "Kileleshwa, Nairobi, Kenya"
1: "Ntinda, Kampala, Uganda"
2: "Uhuru Highway, Nairobi, Kenya"
length: 3
For me to be able to POST the array to the server, I assign the array value to an HTML input field in a form.
document.getElementById('locationArray').value = selecedAddresses;
Find below the code to my HTML form where input field resides:
<form action="/advertiser" method="POST" enctype="multipart/form-data">
<div class="mb-3">
<input type="text" name="locationArray" value="" hidden/>
</div>
<button type="submit" class="btn btn-secondary"> Add location </button>
</form>
When the user clicks on the Add location button, the array is submitted to the server.
And now for the server side code...
app.route('/Geolocations')
.post((req, res)=>{
let locationArray = req.body.locationArray;
console.log("locationArray: " +locationArray);
})
The code above yeilds:
locationArray: "Kileleshwa, Nairobi, Kenya, Ntinda, Kampala, Uganda, Uhuru, Highway, Nairobi, Kenya"
Instead of the desired out put:
['Kileleshwa, Nairobi, Kenya', 'Ntinda, Kampala, Uganda', 'Uhuru Highway, Nairobi, Kenya']
Looking forward to your help.

MeteorJS: How to get id to load from collection

I'm trying to load an array (with simple text) and trying to load it up on the template whenever it is called. How do I get the ID from that specific item to get the array that I stored in it?
HTML Template:
<template name="commentMarker">
<div id="viewMarker">
<h3 id="markerTitle">{{markerName}}</h3>
<h6 id="markerCategory">{{markerCategory}}</h6>
<br>
<fieldset>
<legend>Description</legend>
<p>{{markerDescription}}</p>
</fieldset>
<form id="commentForm">
<fieldset>
<legend>Comments</legend>
<input type="text" id="markerId" name="idForComment" value={{markerId}}>
<textarea rows="3" cols="19" name="comment" id="commentArea" placeholder="Insert your comment here..."></textarea>
{{#each comments}}
<p id="oneComment">{{this}}</p>
{{/each}}
</fieldset>
<input type="submit" value="Comment" class="commentButton">
<input type="submit" value="Close" class="exitButton">
</form>
</div>
</template>
JS:
Template.commentMarker.helpers({
comments(){
alert(template.find("#markerId").value);
if(commentArray.length===0) return;
else return commentArray;
}});
This is where I insert the comment into the collection's item and it's working fine
Template.commentMarker.events({
'click .commentButton': function(e, template){
e.preventDefault();
var id = template.find("#markerId").value;
var comment = template.find("#commentArea").value;
Points.update(id, { $push: { comments: comment }});
commentArray = Points.findOne(id).comments;
template.find("#commentArea").value = ' ';
}
I tried with commentArray as a global variable which still is. But I'm at loss how I can get the Id from that specific item, I even put it's Id (with hidden display) in the form to actually be able to insert the comment. But it doesn't help me with showing the comments because I cannot seem to get to this field in the Template.helpers ...
Not entirely sure what you are trying to do. It's almost like as if you are displaying the comments right after you updated in to the collection. It looks like you are doing this entirely on local and not a online collection.
However, storing it as a session would work...or reactive var. Might not be the best solution tho. Basically replace commentArray = Points.findOne(id).comments; with:
Session.set('comments', Points.findOne(id).comments)
Then to get it out in helpers:
let commentArray = Session.get('comments')
It's not safe to use it all the time tho for sensitive data. Also try catch the findOne(id).comments because it does produce errors if it happen to not find it.
NOTE: If you are going to use Meteor.Methods, you cannot use Session. You have to return the id and find it in your helpers.

Html form (insert xquery) save in xml

I have an html form, when I press the submit button I want to take the information and use the functions of Xquery so that with an (insert) xquery will be saved in the xml.
--html--
<html>
<body>
<form action="insert.xq">
<span ="label">Name:</span>
<input type="text"/>
<span ="label">Telephone:</span>
<input type="text"/>
<span ="label">Website:</span>
<input type="text"/>
<span ="label">Date:</span>
<input type="text"/>
<!-- Buttons submit and reset-->
<input type="submit" value="Submit"/>
<input type="reset" value="Delete"/>
</form>
</body>
</html>
--exist-db XQUERY--
xquery version "3.1";
declare namespace exist="http://exist.sourceforge.net/NS/exist";
declare namespace xmldb="http://exist-db.org/xquery/xmldb";
declare namespace request = "http://exist-db.org/xquery/request";
update insert
<supplier id=" ">
<name> </name>
<telephone> </telephone>
<website> </website>
<date> </date>
</supplier>
into doc("suppliers.xml")//supplier
--xml--
<?xml version="1.0" encoding="UTF-8"?>
<suppliers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="validate.xsd">
<supplier id="1">
<name>Microsoft</name>
<telephone>943715587</telephone>
<website>www.microsoft.com</website>
<date>10/08/2016</date>
</supplier>
</suppliers>
Sorry for my english.
Parameters in the HTTP REST interface are accessed into variables in an XQuery using the request function set as in:
let $name := request:get-parameter("name","")
let $telephone := request:get-parameter("telephone","")
let $website := request:get-parameter("website","")
let $date := request:get-parameter("date","")
Then used as you would in the rest of the code as a XQuery variable:
update insert
<supplier id=" ">
<name>{$name}</name>
...
Now, that is a start. Your HTML needs name attributes on the input elements to pass them as parameters into the query. You need a return after the let's to return and the http request should be set to GET. And not knowing your system, the query and the documents would have to be open to all unless you are in some session-based environment and the HTML GET can find the XQuery based on some pathing in your controller or something.
This is essentially the same as http://path/to/myquery.xm?name=John%20Doe&telephone=123456789 ... and you want to grab the name and telephone.

Match Error with Meteor.callLoginMethod

I'm trying to send my login with :
'submit form': function(event) {
event.preventDefault();
event.stopPropagation();
var loginRequest = {
username: event.target.loginUsername.value.toLowerCase(),
password: event.target.loginPassword.value,
};
var callback = function(response) {
Session.set('showLoading', false);
};
Session.set('showLoading', true);
Accounts.callLoginMethod({
methodArguments: [loginRequest],
userCallback: callback,
});
},
But I get an error and I can't figure out what is the thing that create this error :
Exception while invoking method 'login' Error: Match error: Unknown key in field username
...
Sanitized and reported to the client as: Match failed [400]
I founded some informations in the web but nothing that really helped me. I think it's generated when I call Accounts.callLoginMethod
My form looks like this:
<form>
<div class="row">
<div class="input-field col-xs-12 col-sm-8 col-md-6 col-sm-offset-2 col-md-offset-3">
<label for="loginUsername">Username</label>
<input id="loginUsername" type="text" class="form-control" disabled="{{showLoading}}" required>
<br>
<label for="loginPassword">Password</label>
<input id="loginPassword" type="password" class="form-control" disabled="{{showLoading}}" required>
</div>
</div>
<br>
{{#if showLoading}}
{{> loading}}
{{else}}
<div class="text-center">
<button type="submit" class="btn btn-primary">Login</button>
</div>
{{/if}}
</form>
Someone could help me or know what is creating this error ?
Here is my 2 cents. Accounts.callLoginMethod is technically not a documented API function and in theory could change in any future Meteor release. Since it's not documented, the errors that it returns are not well defined and could be confusing.
Since you are just doing password authentication, I would recommend you use Meteor.loginWithPassword(user, password, [callback]) instead. At least this way you have a set of API documentation to fallback on if you get errors such as this (it also returns more specific errors when something goes wrong).
Try switching​ and see if you still receive an error output. If so the error will be one of the below error messages and you can better debug to see what's going on.
“Unrecognized options for login request [400]” if user or password is undefined.
“Match failed [400]” if user isn’t an Object or String, or password isn’t a String.
“User not found [403]” if the email or username provided in user doesn’t belong to a registered user.
“Incorrect password [403]” if the password provided is incorrect.
“User has no password set [403]” if user doesn’t have a password.
If you encounter one of the above errors, then do console.log(username) and make sure it is a string or object with the value that you are expecting.

Passing a url in a form from page to servlet drops part of query string

If I have a form on a JSP like this:
<form action = "/myApp/myServlet?rssFeedURL=${rssFeedURL}' />" method = "post">
<input type = "button" value = "See data for this RSS feed."/>
</form>
What I find is that if the variable ${rssFeedURL} has no query string, then the server receives it properly, e.g.:
http://feeds.bbci.co.uk/news/rss.xml
But if a query string exists, e.g.:
http://news.google.com/news?ned=us&topic=m&output=rss
I expect that it is to do with the encoding of the '&' character. Can anyone advise?
The server receives only:
http://news.google.com/news?ned=us
My pages are charset=UTF-8 encoded.
You need to URL-encode request parameters. Otherwise they will be interpreted as part of the initial request URL.
JSTL offers you the <c:url> for this.
<c:url var="formActionURL" value="/myApp/myServlet">
<c:param name="rssFeedURL" value="${rssFeedURL}" />
</c:url>
<form action= "${formActionURL}" method="post">
...
An alternative is to create an EL function which delegates to URLEncoder#encode().