Struct knock::HTTP[][src]

pub struct HTTP {
    pub response: Response,
    pub url: Url,
    pub method: String,
    pub body: HashMap<String, Data>,
    pub header: HashMap<String, String>,
    // some fields omitted
}

HTTP struct

extern crate knock;

let mut http = knock::HTTP::new("https://example.com/api/date").unwrap();

Fields

Methods

impl HTTP
[src]

HTTP struct instance

extern crate knock;

let mut http = match knock::HTTP::new("https://example.com/api/date") {
    Ok(http) => http,
    Err(err) => panic!(err)
};

How to send simple GET request

extern crate knock;

let mut http = knock::HTTP::new("https://example.com/api/date").unwrap();
http.get().send();

How to send simple POST request

extern crate knock;

let mut http = knock::HTTP::new("https://example.com/api/date").unwrap();
http.post().send();

How to send simple PUT request

extern crate knock;

let mut http = knock::HTTP::new("https://example.com/api/date").unwrap();
http.put().send();

How to send simple DELETE request

extern crate knock;

let mut http = knock::HTTP::new("https://example.com/api/date").unwrap();
http.delete().send();

Disable SSL validation before performing a GET request

extern crate knock;

let mut http = knock::HTTP::new("https://example.com/api/date").unwrap();
http.danger_accept_invalid_certs(true).get().send();

Send custom Request

extern crate knock;

let mut http = knock::HTTP::new("https://example.com/api/data").unwrap();
http.request("OPTIONS").send();

Send Body data as HashMap<String, Data>

extern crate knock;

use std::collections::HashMap;

let mut http = knock::HTTP::new("https://example.com/api/data").unwrap();
let mut body: HashMap<String, knock::Data> = HashMap::new();
body.insert("key".to_string(), knock::Data::String("value".to_string()));

http.post().body(body).send();

You also can use body_as_str function for sending body as String

extern crate knock;

let mut http = knock::HTTP::new("https://example.com/api/data").unwrap();
http.post().body_as_str("{\"key\": \"value\"}").send();

But you need to set Content-Type in header, default Content-Type

Send Body data as HashMap<String, Data>

extern crate knock;

use std::collections::HashMap;

let mut http = knock::HTTP::new("https://example.com/api/data").unwrap();
let mut body: HashMap<String, knock::Data> = HashMap::new();
let mut header: HashMap<String, String> = HashMap::new();

body.insert("key".to_string(), knock::Data::String("value".to_string()));
header.insert("Content-Type".to_string(), "application/json".to_string());

http.post().body(body).header(header).send();

Send Body data as HashMap<String, Data>

extern crate knock;

use std::collections::HashMap;

let mut http = knock::HTTP::new("https://example.com/api/data").unwrap();
let mut body: HashMap<String, knock::Data> = HashMap::new();

body.insert("key".to_string(), knock::Data::String("value".to_string()));

match http.post().body(body).send() {
    Ok(res) => println!("{:?}", res),
    Err(err) => println!("{:?}", err)
};

Auto Trait Implementations

impl Send for HTTP

impl Sync for HTTP