This page is a guide on how to make a POST request with the HTML form element. First I'm going to explain the difference between POST and GET. Then I'll teach you how to change the method to POST. Next I'm going to explain what a server is and walk you through making a server that acknowledges it received a POST request. After that I'm explaining how to store data and modifying the server to store every request. Finally I will explain what a response is, how every part works, and how the server can send responses with the data it contains.
The intended difference between POST and GET is that POST sends data while GET retrieves it. The practical difference is that POST's request body can transport more data securely. In theory changing the method is as simple as changing GET to POST. In practice POST requests have very little effectiveness unless you add a server that can store requests.
Usually there are two parts to a server, the hardware and the software. Since we're using Val Town we don't need to worry about hardware details like buying or renting a computer. For some reason the industry term for that is serverless despite the fact you need to write server code. I have no idea why. I just memorized that a serverless product is jargon for the company handling all hardware challenges, but not server code despite telling me not to worry about the server. The software aspect technically could only be three parts, but most of the time it needs a fourth part to be useful. The first part is receiving a request. The second part is processing the request. The third part is sending a response. The technically optional part is storing the data.
Now we have a server that accepts requests and processes requests. A HTTP Response is data sent from the server to the client. It always has three parts. The first part is the status line which has three components. The first component is the HTTP version number. The second is a three number response that states if there was a problem and if so what it was. The second part of a HTTP Response are the headers just like the HTTP Request. The final part is the body which contains the requested data. It's usually something like HTML or JSON.
There are five categories of response status codes. The first category 100–199 is informational and means the request is in progress. The second category is 200-299 and means the request is successful. The third category is 300-399 and means the request got redirected. The fourth category is 400-499 and means there's a problem with the request. The fifth category is 500-599 and means there's a problem with the server accepting the request. If you want more details read HTTP response status codes documentation.
There are four types of response headers: Content-Type, Cache-Control, Access-Control-Allow-Origin, and Set-Cookie. Content-Type says what type of data is being sent back to the front end. Cache-Control says if the response is worth saving and how long if it is. Access-Control-Allow-Origin determines if a website is permitted to use the information. Set-Cookie sends new cookies to the browser to store.
The location of the response code is in the if else statement near the end and after the const variable that uses .getAll. The template is
return new Response("<p></p>", { headers: { "Content-Type": "text/html" }, });. Between the paragraph tags, add a sentence stating that the input was received and what the input is. For example "You submitted: Will."The next step is modifying the server so it can store data. It's more complicated than saving data to a variable because variables lose all data every time you save a version. On Val Town there are two ways to store data long term. They are blob and SQlite. Both of them have 10mb on the free plan and 1gb on pro. The difference is that blob storage is just key value while SQlite is a database. Since it doesn't make sense to use SQlite's data unnecessarily and it's complex enough to be its own guide, we're using blob storage.