Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine
This course is archived!
This tutorial uses a deprecated micro-framework called Silex. The fundamentals of REST are still ? valid, but the code we use can't be used in a real application.

HTTP Basics

Video not working?

It looks like your browser may not support the H264 codec. If you're using Linux, try a different browser or try installing the gstreamer0.10-ffmpeg gstreamer0.10-plugins-good packages.

Thanks! This saves us from needing to use Flash or encode videos in multiple formats. And that let's us get back to making more videos :). But as always, please feel free to message us.

HTTP Basics

Yep, we need cover a bit of theory. Wait, come back! This stuff is super important and fascinating too. Put on your thinking cap and let’s get to it!

HTTP

Everything starts with HTTP: the acronym that describes the format of the request message that a client sends and the response that our server sends back. If you think of an API like a function, the request is the input and the response is our output. It’s that simple.

HTTP Request

GET /api/programmers HTTP/1.1
Host: CodeBattles.io
Accept: application/json,text/html

This is a basic request and it has 3 important pieces:

  1. /api/programmers is the URI: uniform resource identifier. I said resource! Each URI is a unique address to a resource, just like you have a unique address to your house. If you have 5 URI’s you’re saying you have 5 resources.
  2. GET is the HTTP method and describes what action you want to take against the resource. You already know about GET and POST, and possibly also DELETE, PUT and the infamous PATCH. There are others, but mostly we don’t care about those.
  3. Every line after the first is just a colon-separated list of headers. This request only has two, but a client could send anything.

With that in mind, a POST request might look like this:

POST /api/programmers HTTP/1.1
Host: CodeBattles.io
Authorization: Bearer b2gG66D1Tx6d89f3bM6oacHLPSz55j19DEC83x3GkY
Content-Type: application/json

{
    "nickname": "Namespacinator"
}

It’s the same, except the method is POST and we’re sending data in the body of the request. We also have 2 extra headers, one for authentication and one that tells the server that the body has JSON-formatted stuff in it.

HTTP Response

The response message is similar:

HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: no-cache, private

{
    "nickname": "Namespacinator",
    "avatarNumber": 5,
    "tagLine": "",
    "powerLevel": 25
}

The 200 status code is the first important piece, and of course means that everything went just great. Status codes are a big part of APIs. But people also like to argue about them. We’ll see the important ones as we build.

The headers tell the client that the response is JSON and that the response shouldn’t be cached. And of course, the JSON body is at the end.

HTTP is awesome and really simple. Got it? Great, let’s move onto something harder.

Leave a comment!

3
Login or Register to join the conversation
Pilgougou Avatar
Pilgougou Avatar Pilgougou | posted 2 years ago

Hey! I'm sorry, I don't understand spoken english very well. I see the script bottom the video but it's not very practicable to scroll down and up everytime. Can you activate subtitles on theses API series?
Thank's you so much, and incredible project!!!

Reply

Hey Pilgougou

Hey I'm sorry that you got some troubles with learning. I'll check if we can do something with it, this course is pretty old and I'm not sure about subtitles here. As a fast solution I can advice to use Picture-in-picture feature.

Cheers!

Reply

Yup, sadikoff is correct! Unfortunately, this tutorial is getting pretty old (the concepts are still great, but the code is old). And it predates our subtitles. There's not a reasonably easy way for us to add them :/.

Sorry I can't give you a better answer! But the vast majority of the tutorials on the site DO have real subttiles.

Cheers!

Reply
Cat in space

"Houston: no signs of life"
Start the conversation!

This tutorial uses a deprecated micro-framework called Silex. The fundamentals of REST are still ? valid, but the code we use can't be used in a real application.

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "silex/silex": "~1.0", // v1.3.2
        "symfony/twig-bridge": "~2.1", // v2.7.3
        "symfony/security": "~2.4", // v2.7.3
        "doctrine/dbal": "^2.5.4", // v2.5.4
        "monolog/monolog": "~1.7.0", // 1.7.0
        "symfony/validator": "~2.4", // v2.7.3
        "symfony/expression-language": "~2.4" // v2.7.3
    },
    "require-dev": {
        "behat/mink": "~1.5", // v1.5.0
        "behat/mink-goutte-driver": "~1.0.9", // v1.0.9
        "behat/mink-selenium2-driver": "~1.1.1", // v1.1.1
        "behat/behat": "~2.5", // v2.5.5
        "behat/mink-extension": "~1.2.0", // v1.2.0
        "phpunit/phpunit": "~5.7.0", // 5.7.27
        "guzzle/guzzle": "~3.7" // v3.9.3
    }
}
userVoice