Chapter 2. HTTP Verbs
HTTP verbs such as GET
and POST
let us send our intention along with the URL so we can instruct the server what to do with it. Web requests are more than just a series of addresses, and verbs contribute to the rich fabric of the journey. This chapter covers how to make and respond to HTTP requests using a selection of common HTTP verbs, including lots of examples.
I mentioned GET
and POST
because it’s very likely you’re already familiar with those. There are many verbs that can be used with HTTP—in fact, we can even invent our own—but we’ll get to that later in the chapter (see “Using Other HTTP Verbs”). First, let’s revisit GET
and POST
in some detail, looking at when to use each one and what the differences are between them.
Serving GET Requests
URLs used with GET
can be bookmarked, they can be called as many times as needed, and the request should change the data it accesses. A great example of using a GET
request when filling in a web form is when using a search form, which should always use GET
. Searches can be repeated safely, and the URLs can be shared.
Consider the simple web form in Figure 2-1, which allows users to state which category of results they’d like and how many results to show. The code for displaying the form and the (placeholder) search results on the page could be something like this:
<html>
<head>
<title>
GET Form</title>
<link
rel=
"stylesheet"
href=
"http://yui.yahooapis.com/pure/0.6.0/pure-min.css"
>
</head>
<body>
<div
style=
"margin: ...
Get PHP Web Services, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.