How it works...

In this recipe, we created a Rust project named nickel-jsonhandling, which helps us get data from the end user and perform a certain set of actions based on the input. The nickel crate makes it easy to map JSON data right onto your struct. We use the rustc-serialize dependency for this project in order to handle JSON encoding and decoding.

Starting at the top, we referenced the external nickel crate using the extern keyword and loaded all of its macros with #[macro_use]. We use nickel as the application object and surface, which holds all the public APIs; it's basically a struct that implements all the fundamental methods for performing the web application tasks. The HttpRouter is a public trait provided by the nickle crate, which has the signature of various REST API calls. JsonBody is a public trait provided by the nickle crate, which has the signature of the json_as method, which takes a decodable type provided by the rustc-serialize crate. We create a custom struct type named Person, which has two string fields: firstname and lastname. The JSON body that is posted from the end user is converted to the Person type so that we can use it our application. To be able to encode a piece of data, it must implement the rustc_serialize::Encodable trait. To be able to decode a piece of data, it must implement the rustc_serialize::Decodable trait. The Rust compiler provides an annotation to automatically generate the code for these traits: #[derive(RustcDecodable, RustcEncodable)].

In the main function, we first assign server instances to a mutable variable and create a new nickel application object with Nickel::new(), which creates an instance of nickel with default error handling.

The server.post method registers a handler to be used for a specific POST request. Handlers are assigned to paths and paths are allowed to contain variables and wildcards. A handler added through this API will be attached to the default router. The middleware! macro reduces the amount of boilerplate code needed for each route. We create a variable, person, which is assigned to request.json_as::<Person>().unwrap(), where request is a parameter containing the information from the end user and the unwrap method is one of the several ways that Rust provides for assigning a value. We provide a simple message, "Hello {} {}", person.firstname, person.lastname", in the format! macro to be displayed when the /a/post/request endpoint is accessed, where person is a variable of the Person type.

To hit the endpoint, we use the curl command, where -H stands for the header type, which is"Content-Type: application/json", and we give a POST request (-X) at http://127.0.0.1:6767/a/post/request with the following data (-d): '{"firstname":"Vigneshwer","lastname":"Dhinakaran"}'.

The server.listen method listens to the API requests on 127.0.0.1:6767 where it binds and listens for connections on the given host and port.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.104.95