How to test form submission - go

I have a login form with some html that users fill in:
<form method="post" enctype="multipart/form-data">
<div class="form"><input name="username"> </div>
<div class="form">
<button type="submit">Login</button>
</form>
I would like to test that the username is being received correctly. I am able to send a multipart form however this is contained within the body of the request not as a postform.
form := url.Values{}
form.Set("username", "Bob")
// Create request
req, _ := http.NewRequest("POST", "/login", strings.NewReader(form.Encode())) // sends as multipart form in request body
req.Header.Set("Content-Type", "application/www-form-urlencoded")
testRouter.ServeHTTP(rr, req)
How do I make a request with the form data attached as a Postform so that I can extract it c.Postform("username") from the backend as if submitted via the browser?

keep button in the form with button type submit
<form method="post" enctype="multipart/form-data">
<div class="form">
<input name="username">
<input type="submit" name="submit" value="submit"/>
</div>
</form>

Figured it out. Turns as you can simply do req.PostForm = form....

Related

Spring Boot Thymeleaf form post data than json data

My problem is I have thymeleaf template for reset password and I want passwords post than json data (actualy post than form data) to my spring boot server. How can I do it?
My rest controller:
#PostMapping(value = "/save-new-password")
public String saveStudent(#Valid #RequestBody NewPasswordDTO newPasswordDTO) {
return "OK";
}
Part from my reset-password-page.html:
<body>
<form th:action="#{/save-new-password}" th:object="${newPasswordDTO}" method="post">
<div class="box">
<h1>RESET PASSWORD</h1>
<input class="password" name="newPassword" type="password" placeholder="New password" th:field="*{newPassword}">
<input class="password" name="confirmPassword" type="password" placeholder="Confirm password" th:field="*{confirmPassword}">
<input class="button" type="submit" value="RESET"/>
</div>
</form>
</body>

My form doesn't submit image file to controller store function, What is the problem

I want to send an image file to the controller store method, but the input file sends a temp file what is the problem, anyone can help me?
My Form:
<form method="POST" action="{{route('books.store')}}" enctype="multipart/form-data" >
#csrf
#method('POST')
<input type="file" name="cover_image" value="{{old('cover')}}">
<input type="submit" class="" value="send">
</form>
when I return:
return $request->cover_image;
the result is :
C:\xampp\tmp\phpF61B.tmp

Update method in html form in laravel

I'm trying to update inputs using html form in laravel:
<form action="{!! route('users.update',['id' => $users->id]) !!}" method="post">
<div class="form-group row">
<label for="colFormLabelLg" class="col-sm-3 col-form-label col-form-label-lg">customer_name</label>
<div class="col-sm-10">
<input value="{{$name}}" class="form-control form-control-lg" placeholder="col-form-label-lg">
</div>
<button type="submit" class="btn btn-primary btn-lg" > Edit</button>
</form>
Everything in the controller work perfectly however in the view page I received this error:
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
No message
What am I doing wrong?
Please correct your route as POST like:
Route::post('update/{id}', 'YourController#update')->name('users.update');
You need to spoof the method as you are using to post the data. Since HTML forms can't make PUT, PATCH, or DELETE requests, you will need to add a hidden _method field to spoof these HTTP verbs. The #method Blade directive can create this field for you like this:
<form action="/foo/bar" method="POST">
#method('PUT') //add this to your form
</form>
or
<form action="/foo/bar" method="POST">
{{ method_field('patch')}} //add this to your form
</form>
You need to put #csrf and #method('PATCH') inside your form view.
I have the same problem and I had it worked after adding some lines in my code:
Just use $users->id instead of doing it as array ['id' => $users->id]
Use csrf and spoof the method by adding #method('PUT')
Your code should look like this:
<form action="{!! route('users.update', $users->id) !!}" method="post">
#csrf
<!--Some fields-->
#method('PUT')
</form>

Spring or thymeleaf does not pass the id parameter to the request?

So there is the following form:
<form method="get" th:action="#{/aliments/edit(id=${aliment.id})}">
<input th:type="submit" value="Edit"/>
</form>
Which generates the following html in the browser:
<form method="get" action="/aliments/edit?id=1">
<input value="Edit" type="submit">
</form>
The problem is that when the form is submited (by clicking the 'Edit' btn) the following request is made (http://localhost:8080/aliments/edit? instead of http://localhost:8080/aliments/edit?id=1):
So why the id parameter is not passed to the request?

AJAX form not returning data when using input file

im experiencing a weird issue. I have this form basically it just sends data to another cfm file that processes the inputs and updates a database. Today ive added an input type="file" so to process EXIF data from it and extract GPS info. The result page will copy a div to the specified target div. All works fine, data gets extracted and updated, but the div with the response does not appear anymore. As soon as i remove the input="file" div target div gets updated. Seems a response header issue, but i have no idea how to fix it. Thanks.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
<script>
$(document).ready(function() {
// bind form using ajaxForm
$('#sede-form').ajaxForm({
// target identifies the element(s) to update with the server response
target: '#htmlExampleTarget',
// success identifies the function to invoke when the server response
// has been received; here we apply a fade-in effect to the new content
success: function() {
$('#htmlExampleTarget').fadeIn('slow');
}
});
});
</script>
<cfoutput query="dett_cli">
<form id="sede-form" enctype="multipart/form-data" action="modifica_sede_response.cfm" method="post" class="form-inline">
<input type="hidden" name="codice" value="#url.codice#" />
<input type="hidden" name="id_sede" value="#url.id_sede#" />
... [other fields]...
<input type="hidden" name="sed_coordinate_o" value="#sed_coordinate#" class="input-large">
<div class="control-group">
<label class="control-label" for="sed_coordinate">JPG Coordinate GPS </label>
<div class="controls">
<div class="input-prepend">
<input type="file" name="sed_coordinate" id="sed_coordinate" class="input-large">
</div>
</div>
</div>
... [other fields]...
<!-- END div.row-fluid -->
<div class="form-actions">
<button type="reset" class="btn btn-danger"><i class="icon-repeat"></i> Resetta</button>
<button type="submit" class="btn btn-success"><i class="icon-ok"></i> Aggiorna</button>
<div id="htmlExampleTarget"></div>
</div>
</form>
</cfoutput>
Try putting the following back into your ajaxForm options.
iframe:true,
forceSync:true,

Resources