Cannot POST / With Post request from non '/' site - html

I am new to Node and am having trouble with the POST functionality.
I have a simple website, where I have the following lines:
app.get('/sign',(req,res)=>{
res.sendFile(path.join(__dirname, 'static', 'index.html'));
});
app.post('/sign', (req,res)=>{
res.send("Success!")
});
The get request works perfectly fine, but when I post the data in the form back, instead of getting a success message, I get Cannot POST /.
When doing the same from the '/' directory with app.get('/', etc.)
it works fine.
This is the content of the 'index.html' file:
<form action = '/' method='POST' id="form">
<div class="form-group">
<label for="emal">Email address</label>
<input type="email" class="form-control" name="email">
</div>
<div class="form-group">
<label for="pwd">Password</label>
<input type="password" class="form-control" name="password">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>

The action attribute specifies the URL the form data will be posted to.
You have said to post it to /.
Your server side code provides a route for /sign but not /.
You need your URLs to match. Either change action="/" to action="/sign" to be explicit about where you are posting it to or remove the action attribute entirely so it posts to the current url (which is /sign since that is the route of the GET handler that provided the HTML document containing the form).

Related

not able to view the JSON format of the data after submitting the form

i am working on angular project where i have to fill the username and password
after submitting the code i have routed to swagger URL but i am not able to get the JSON format data
my form
<!-- HTML code for the login form -->
<form #form="ngForm" id="login-form" (ngSubmit)="addUsers()">
<label for="username">Username:</label><br>
<input type="text" id="username" name="username" [(ngModel)]="addUser.userName"><br>
<label for="password">Password:</label><br>
<input type="password" id="password" name="password" [(ngModel)]="addUser.userPassword"><br><br>
<input type="submit" value="Submit">
</form>
my typescript
addUsers(){
this.userService.addUser(this.addUser)
.subscribe({
next:()=>
this.router.navigateByUrl("https://localhost:7068/Users")
})
even i added service too check this out
My service
addUser(adduser:User):Observable<User>{
adduser.userName
adduser.userPassword
return this.http.post<User>(this.baseApiUrl+"/Users",adduser)
}
i even tried to route using new component too but i failed please help me out
THANKS IN ADVANCE

get input form not as variable but as url

I am building a webservice so people can search into the database. Lets say I have users and companies. Each user and company can be found thought their id. So if you search myurl/users/<id> you get information of that user, on the other hand if you search company/ you get information of that company.
For this I have created two simple input texts (one for users and another for companies) where people can type the <id>. My problem is that when I get the value from the input text I get this myrul/users?<id> and not myurl/users/id. I tried to hardcode the slash but then I get myrul/users/?<id>.
So my question is how can I get input text as a url and not as a variable.
I am using flask so my html has jinja2 code like this:
<!-- USER id -->
<form method='GET' action={{url_for('get_info_by_id', type_collection='user')}}>
<input type="text" name="my_id"/><input type="submit" value="Go">
</form>
<!-- COMPANY id-->
<form method='GET' action={{url_for('get_info_by_id', type_collection='company')}}>
<input type="text" name="my_id"/><input type="submit" value="Go">
</form>
In my python script (flask)
#app.route('myurl/<type_collection>/<my_id>')
get_info_by_id(type_collection,my_id):
# search into the database and return info about that id
As #dirn suggested in the commentary, I made it through JavaScript, here is the code if someone else is also interested:
HTML:
<!-- USER id -->
<form method='GET' class="search" id="user" action="">
<input type="text" name="my_id"/><input type="submit" value="Go">
</form>
<!-- COMPANY id-->
<form method='GET' class="search" id="company" action="">
<input type="text" name="my_id"/><input type="submit" value="Go">
</form>
JS:
$(".search").submit(function( event ){
event.preventDefault();
var my_id = $(this).find(":input").val();
url = 'myurl/'+ $(this).attr("id") + '/' + my_id;
window.location.href = url;
});
python (flask)
#app.route('myurl/<type_collection>/<my_id>')
get_info_by_id(type_collection,my_id):
# search into the database and return info about that id
Is there a reason you cannot use the variable, or are you just trying to get it into a URL so that you can do your search? I went out on a limb and assumed you just want the form and database search to work, so try the following out.
Adjust your route like so:
#app.route('myurl/<type_collection>/')
def findAllTheThings():
if not request.form['my_id']: # Just check if a specific entity is chosen
return render_template('YourTemplateHere') # If no entity, then render form
entity_id = request.form['my_id']
get_info_by_id(type_collection, entity_id):
# search into the database and return info about that id
Now adjust the template as follows:
<!-- USER id -->
<form method='GET' action={{url_for('findAllTheThings', type_collection='user')}}>
<input type="text" name="my_id"/><input type="submit" value="Go">
</form>
<!-- COMPANY id-->
<form method='GET' action={{url_for('findAllTheThings', type_collection='company')}}>
<input type="text" name="my_id"/><input type="submit" value="Go">
</form>
Now, if no entity has been selected you'll just render the form. You can throw in a flash to let them know they need to select a specific ID, or just let them figure it out. If an entity has been selected, you will call the fucntion correctly.

Passing content to a URL (Who.is)

I am going to need to add a form to a page on a site, that takes a domain name as the input from the viewer and sends it to who.is so that the DNS for this domains a returned.
When searching onwho.is, the URL for the search query when looking for me.com looks like this:
http://who.is/dns/me.com
I am struggling here since I can't pass the domain name using the variable form, with ?q=...
Any Ideas?
This is so far the html I have for this form:
<form method="get" action="http://who.is/dns/" target="_blank">
<input type="text" name="q" size="31" maxlength="255" placeholder="Who is" width="255"/>
</form>
Thanks
You will not be able to do this in HTML unless you find a URL with parameters, like TechnoKnol posted.
You need this or a server process
window.onload=function() {
document.getElementById("who").onsubmit=function() {
window.open("http://who.is/dns/"+document.getElementById("q").value(),"_blank");
return false;
}
}
using
<form id="who">
<input type="text" id="q" size="31" maxlength="255" placeholder="Who is" width="255"/>
</form>
By inspecting on who.is, I found below url
http://who.is/search.php?search_type=Whois&query=me.com&commit=Search
With some modification in your form will work.

HTML generate URL from Input

i am working on a search function, herefore i need the value of an input to generate the final url (which shows the results)
Let's say the user enters the content he is looking for here:
Name: <input type="text" id="myText">
now i need to generate a hyperlink from
http://constant/constant?query=NAME&someotherconstantthings
here, the NAME needs to be replaced from the content of the input
Try to use PHP, you could add a action to the Form Element to post the entered informations to the PHP file, then generate ur hyperlink.
<form action="phpfilename.php" method="post">
Name: <input type="text" id="myText" name="myText">
<input type="submit" value="Search">
</form>
<?php
$name = $_POST['myText'];
$hyperlink = 'http://constant/constant?query='.$name;
?>
You need something like this:
<form action="URL" method="get">
Enter your name here: <input type="text" name="query" value="">
<input type="submit" value="Search">
</form>
You need to replace the keyword URL with the path to the page which performs the search. You can remove the keyword URL if want to submit the form to the same page.

HTML Form element to URL

I am using an eCommerce engine script that uses a different search method.
Instead of a URL using GET like this:
http://search.com/searchc?q=the+query
It uses
http://search.com/searchc/the+query
How can I make a form to POST or GET to that, because this form makes the URL
http://search.com/searchc/?q=the+query
<form action="/searchc/" method="post">
<input type="text" id="q" name="q">
<input type="submit" value="go">
</form>
Also tried this (get or post do not work for both of these)
<form action="/searchc/" method="post">
<input type="text" id="" name="">
<input type="submit" value="go">
</form>
The reliable way has two components: Client-side JavaScript manipulation, which turns form submission to a request as needed, and (as backup for non-JS situations) a simple server-side redirect utility which receives a request from the form and redirects it as modified.
Something like this (for the GET case):
<form action="http://www.example.com/redirect"
onsubmit="location.href = document.getElementById('f1').value +
document.getElementById('q').value; return false">
<input type="text" id="q" name="f2">
<input type="submit" value="go">
<input type=hidden id=f1 name=f1 value="http://search.com/search/">
</form>
Here http://www.example.com/redirect is some server-side form handler that just reads the form fields and picks up fields named f1, f2,..., concatenates them into a single string, and redirects using it as a URL. As a CGI script, this would be
use CGI qw(:standard);
$dest = '';
$i = 1;
while(param('f'.$i)) {
$dest .= param('f'.$i++); }
print "Location: $dest\n\n";
<form action="/searchc/" method="post" onsubmit="this.action+=this.q.value;return true">
<input type="text" id="q">
<input type="submit" value="go">
</form>
spaces will be submited as %20
you can use
this.action+=this.q.value.split(' ').join('+')
to replace them
This is very strange url pattern, but anyway you could do something like:
$(function () {
$('form').submit(function () {
var url = '/searchc/' + encodeURIComponent($(this).find('[name=q]').val());
window.location = url;
return false;
});
});