dnes vás provází Lukáš Grolig
Browser
Webserver
HTTP
GET / HTTP/1.1
Host: www.example.com
Method
Location
Protocol version
Host name
příklady requestů
GET styles.css HTTP/1.1
GET /api/tasks HTTP/1.1
GET /page.html HTTP/1.1
HTTP/1.1 200 OK
Date: Mon, 23 May 2005 22:38:34 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 138
Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
Server: Apache/1.3.3.7 (Unix) (Red-Hat/Linux)
ETag: "3f80f-1b6-3e1cb03b"
Accept-Ranges: bytes
Connection: close
<html>
<head>
<title>An Example Page</title>
</head>
<body>
<p>Hello World, this is a very simple HTML document.</p>
</body>
</html>
GET
HEAD
OPTION
PUT
DELETE
POST
}
Idepotent verbs
non-idepotent
{
safe
200
204
301
400
401
403
404
500
OK
No Content
Moved Permanently
Bad Request
Unauthorized
Forbidden
Not Found
Internal Server Error
vracím data
něco se stalo, ale bez obsahu
vždy přesměrovat na novou URL
špatné typy, rozsahy, apod
je potřeba se přihlásit
přihlášený nemá přístup
stránka neexistuje
nějaká chyba u nás
{
"teams": [
{
"name": "Seven Deadly Sins",
"heroes": [
{
"name": "Meliodas",
"powerLevel": 60000,
"race": "Demon"
},
{
"name": "Elizabeth",
"powerLevel": 60000,
"race": "Goddess"
},
{
"name": "Bane",
"powerLevel": 30000,
"race": "Human"
}
]
}
]
}
/devices /configurations
/devices/{id} /configurations/{id}
/devices/{id}/configurations /devices/{id}/configurations/{id}
GET /getAllEmployees
GET /getEmployeeWithId
POST /createEmployee
GET
PUT
DELETE
POST
}
Idepotent verbs
non-idepotent
safe ->
[dependencies]
actix-web = "3"
use actix_web::{get, post, web, App,
HttpResponse, HttpServer, Responder};
#[get("/")]
async fn hello() -> impl Responder {
HttpResponse::Ok().body("Hello world!")
}
#[post("/echo")]
async fn echo(req_body: String) -> impl Responder {
HttpResponse::Ok().body(req_body)
}
async fn manual_hello() -> impl Responder {
HttpResponse::Ok().body("Hey there!")
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.service(hello)
.service(echo)
.route("/hey", web::get().to(manual_hello))
})
.bind("127.0.0.1:8080")?
.run()
.await
}
use actix_web::{post, web, HttpResponse};
use serde::Deserialize;
#[derive(Deserialize)]
struct FormData {
username: String,
}
#[post("/")]
async fn index(form: web::Form<FormData>) -> HttpResponse {
HttpResponse::Ok().body(
format!("username: {}", form.username)
)
}
use actix_web::{web, App, HttpServer, Result};
use serde::Deserialize;
#[derive(Deserialize)]
struct Info {
username: String,
}
/// extract `Info` using serde
async fn index(info: web::Json<Info>) -> Result<String> {
Ok(format!("Welcome {}!", info.username))
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(
|| App::new().route("/", web::post().to(index)))
.bind("127.0.0.1:8080")?
.run()
.await
}
use actix_web::{get, web, HttpResponse, Result};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct MyObj {
name: String,
}
#[get("/a/{name}")]
async fn index(obj: web::Path<MyObj>) -> Result<HttpResponse> {
Ok(HttpResponse::Ok().json(MyObj {
name: obj.name.to_string(),
}))
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
// as usual
}