Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POST contents of a form to rest api

I have made a html user login form:

<html >
<head>

<title>User Login</title>
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="css/signin.css">
</head>

<body>
<div class="container">
<form class="form-signin">
<h2 class="form-signin-heading">Please sign in</h2>
<input class="form-control" type="text" autofocus="" required="" placeholder="Email address">
<input class="form-control" type="password" required="" placeholder="Password">
<label class="checkbox">

</label>
<a href= "" <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button></a>
</form>
</div>
</body>
</html>

I now need to send the contents of the form, ie the username and password, to a rest api for authentication using the http POST command. I have no idea where to start with this and am having trouble finding a tutorial which assumes absolutely no knowledge of rest APIs and http commands. I'm extremely new to web development, can anyone point me in the direction of a good tutorial, or show me an example of what the javascript (I presume) might look like?

like image 859
bookthief Avatar asked Nov 12 '13 15:11

bookthief


People also ask

Can I use post to get data in REST API?

GET requests should be used to retrieve data when designing REST APIs; POST requests should be used to create data when designing REST APIs. Creating something is a side effect — if not the point. The HTTP GET method isn't supposed to have side effects. It's considered read-only for retrieving data.

How do you post data from a form?

To post HTML form data to the server in URL-encoded format, you need to make an HTTP POST request to the server and provide the HTML form data in the body of the POST message. You also need to specify the data type using the Content-Type: application/x-www-form-urlencoded request header.

How do you get information from a form when method is post?

The method attribute specifies how to send form-data (the form-data is sent to the page specified in the action attribute). The form-data can be sent as URL variables (with method="get" ) or as HTTP post transaction (with method="post" ). Notes on GET: Appends form-data into the URL in name/value pairs.


1 Answers

First fix your HTML. Then with the following no javascript is necessary:

<form class="form-signin" method="POST" action="URL_OF_REST">
  <h2 class="form-signin-heading">Please sign in</h2>
  <input class="form-control" type="text" required name="email" placeholder="Email address">
  <input class="form-control" type="password" required name="password" placeholder="Password">
  <label class="checkbox"></label>
  <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>
like image 72
carter Avatar answered Oct 30 '22 11:10

carter