Passing arguments to POST Method - html

Is that possible to pass arguments to a HTML form in POST method by modifying the URL? If so how can I do it?

Arguments in the URL will be passed via the GET method. To pass via POST, you need to construct and post a form.
If you wish to dynamically add fields to a form, you can do this by creating a new hidden input element in Javascript and appending it to your form. JSFiddle demo here.

No, that's impossible.
The GET method is passed via the URL. The POST method is sent using a form. You can in fact use both at the same time.

no, that's not possible.
"The HTML specifications technically define the difference between "GET" and "POST" so that former means that form data is to be encoded (by a browser) into a URL while the latter means that the form data is to appear within a message body."
http://www.cs.tut.fi/~jkorpela/forms/methods.html

You can do something like this:
<form method="POST" action="http://example.com?this=that">
<input type="text" name="textfield">
</form>
and retrieve the this=that value server-side. But generally speaking, mixing GET queries in a url with a POSTed form is generally frowned on. Especially if you've got duplicated field names in the url AND the form body.

I strongly recommend using a javascript library, such as jquery but here's the old school:
<script type="text/javascript">
function fillForm() {
document.getElementById('myText').setAttribute('value', 'some value for myText');
// this would send the form:
// document.getElementById('myForm').submit();
return true;
}
</script>
<form method="POST" action="formHandler.php" id="myForm">
<input type="text" id="myText" name="myText" value="" />
<input type="submit" value="Send" />
</form>
<button onclick="fillForm();">Fill it!</button>

There is a way in which you could pass arguments to POST method in bash(command line of course). Use wget tool. example :- wget somewebsite.com/login --post-data="username=name&password=password"

Related

FastAPI: Is there a way to use an HTML form to act as the path parameters for an endpoint?

I have a GET function written to grab a file from an endpoint given namespace and project parameters. It looks a little something like this:
#app.get('/{namespace}/{project}')
async def get_project(namespace, project):
# this then uses the response module...
I want submitted content of my HTML form to become the {namespace} and {project} values, but I can't find a way to do so. So far, my code looks like this:
<form action="/{namespace}/{project}" enctype="multipart/form-data" method="get">
<input type="text">
<input type="submit">
</form>
Is there any way to do this with just HTML and FastAPI?
You can receive it in the post method.
Why are you using the GET method?
And if you need to use GET method, you can you
<a herf="/{namespace}/{project}"> </a>
I hope it will help you to solve this issue.

HTML form does not submit correctly

I am using Laravel for my back end.
This does not work
<form action="/promo/update" style="background-color: #fafafa" method="GET">
This works
<form action="/promo" style="background-color: #fafafa" method="GET">
Is there something about using a GET and an extra "/"?
.. Is there something about using a GET and an extra "/"?
Short answer? No.
As others have noted, just make sure your routes file is expecting the correct parameters. I'd also suggest reading here to learn about PUT vs POST, and here to learn about form method spoofing.
Generally speaking, an UPDATE endpoint should be a POST or PUT. That's not your question though, so I'll just stick to answering what you have here.
Routes/web.php
Route::get('/promo/update', 'PromoController#update')->name('promo.update');
Blade
<form method="GET" action="/promo/update">
#csrf <!-- remember to pass your token -->
<input type="text" name="foo" id="foo" class="form-control" />
<button type="submit" class="btn btn-primary">Submit Form</button>
</form>
PromoController
public functon update( Request $request )
{
// Process your data
}
Because your route's method (/promo/update) is post and you use get...You can see available routes methods just by run below code in terminal:
php artisan route:list
First, make sure you have made the route in your routes file.
Second...
Laravel automatically generates a CSRF "token" for each active user session managed by the application. This token is used to verify that the authenticated user is the one actually making the requests to the application.
Anytime you define an HTML form in your application, you should include a hidden CSRF token field in the form so that the CSRF protection middleware can validate the request. You may use the #csrf Blade directive to generate the token field:

How to hide parameters in URL without javascript?

My form is like this:
<form method="GET" action="target">
<input type="text" name="filter"/>
<select name="skill">
<option>opt1</option>
<option>opt2</option>
<option>opt3</option>
</select>
<input type="number" name="level"/>
<input type="submit"/>
</form>
I would like the empty parameters not to be shown in the URL when the form is submitted, and this happens with the skill field , but not with level and filter, which are added to the Query string even though they're empty. How does it happen? Is it possible to prevent this from happening without using javascript?
None of these fields is required to submit the form.
This is not possible without javascript.
You can use something like jQuery to accomplish this.
note: Using form method POST is the preferred way of submitting forms, and the parameters are submitted as http request body and not in the URL.
If you do a form POST instead of GET, the form params will not appears in the query string. But that would require back end changes.

Input field in GET form: don't supply parameter in URL?

I'd like to submit a form using GET but without passing the value of an input field in the URL.
(Some background may be helpful: I'd like to use a form so that the user can enter their name and press Enter in order to proceed, but all I actually want them to do is go to the next page, and it's important the URL doesn't have parameters in.)
Currently the displayed URL of the next page /nextpage?username=fred:
<form name="entername" action="/nextpage" method="get">
<label for="name">Enter your name: </label>
<input id="username" name="username"><br/>
<input id="submitme" type="submit" value="Submit" class="button"></form>
Is there a way it could go simply to /nextpage instead?
I guess "use POST" is one answer, but I'd rather avoid that if possible (because then if the user refreshes the next page, they get a scary 'Resubmit information?' alert).
So I'm just wondering if there's any other way besides POST.
The proper solution will be to make a POST request, and when you process it, to redirect the user to the answer page, thus causing the user's browser to make a GET request to it.
This ensures that if the user refreshes his answer page, it will not cause a resubmition alert . See more detailed description of this POST/REDIRECT/GET pattern
The somewhat hacky solution will be to use onsubmit javascript handler, which will set some cookies reflecting the form input fields, and then will remove the fields, so that they will not appear in the user's urlbar. The server should then read the submited values from the cookies.
Here is an example of the cookie hack, using jquery and a php script as a backend processor:
<?php
//unsets the cookie:
setcookie('username', "", time() - 3600);
?>
<!DOCTYPE html>
<body>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<form name="entername" action="" method="get">
<label for="name">Enter your name: </label>
<input id="username" name="username"><br/>
<input id="submitme" type="submit" value="Submit" class="button">
</form>
<script>
function js_setcookie(c_name,value){
document.cookie=c_name + "=" + escape(value);
}
$(function(){
$('form').submit(function(){
var username=$("#username").val();
js_setcookie("username", username);
window.location = ""; // "" means GET the same page ... change it to whatever backend URL you want instead, for example "http://..../nextpage" .
return false; // prevent the default event handler that will make a GET request with a query string in it.
});
});
</script>
<?php
echo "<pre>";
var_dump(isset($_COOKIE['username'])?"Cookie: ".$_COOKIE['username']:"Cookie is not set.");
echo "</pre>";
?>
</body>
</html>
You could delete the input field using Javascript with an onsubmit handler.
You can implement an iframe, and in that iframe you do the dirty stuff. That is really old school and not really an nowdays pattern.
I would save all information inside a Cookie or Session, from page to page using Ajax. And not post nor get, just link to the next step.
If you need to get the value on the server you can catch the value in the script "/nextpage" then make a redirect to a page without parameters. But i don't like this workaround. Better use POST or process form with help of JavaScript/Ajax.
That is simply impossible. It is just how GET requests work. You use the query part of the URL for passing parameters.

Submitting an HTML form with a missing parameter name

Normally an HTML form sends query parameters as key-value pairs like this:
http://blabla/?something=something&this=that
But what I need is a form that generates a URL with one of the query keys omitted:
http://blabla/?something&this=that
As far as I can tell, a missing or empty name attribute does not quite provide what I expect:
<input type="hidden" name="" value="myvalue"/>
Leads to this, with an equals sign that I don't want:
http://blabla/?=myvalue
I know it's not good practice to do this, but I need to interface with an existing poorly-designed system.
If you need the attribute to not have a value, shouldn't you do something like this instead?
<input type="hidden" name="something" value=""/>
which would produce the URL http://blabla/?something=&this=that that you are looking for, only with the '=' after something. Or, just leave it out entirely (ie, do not define an input type hidden) and you would get the URLhttp://blabla/?this=that ...
Maybe I'm missing the point here, but either just don't submit that value or set it to null prior to submitting the form. It's not good practice to have an input without a name, so keep the name.
Obviously, we don't know how the script that accepts the form input is setup, but in my experiences unless some sort of crazy server-side validation was setup, it shouldn't bark at you.
These answers make sense logically, but unfortunately this system is very picky about which characters it will accept and any spurious equals signs give it trouble. It's an Innovative Interfaces library OPAC, by the way.
I figured out one way to do it, which is by not submitting the form at all but using JavaScript to inject the contents of the text box into a dynamically-generated URL and then opening that using window.location:
<form name="search_form" method="get" action="">
<input type="text" size="30" maxlength="100"/>
<input type="submit" value="Search" />
</form>
<script type="text/javascript">
window.onload = function() {
document.search_form.onsubmit = function() {
var term = document.search_form.elements[0].value;
var url = "http://blabla/search/X?d:(electronic books) and ("
+ term + ")&searchscope=1";
window.location = url;
}
}
</script>
I don't do much JavaScript and this will certainly cause alarm to anyone mindful of accessibility and web standards compliance. However, rest assured it is no worse than any of the rest of the javascriptaghetti that is part of this system.