Unirest is a lightweight HTTP client library that simplifies making HTTP requests in multiple languages, including Python, Ruby, Java, and PHP. With an easy-to-use API, Unirest allows developers to send HTTP requests and receive responses effortlessly.
About Unirest
Unirest is an open-source project that aims to provide a consistent set of HTTP request methods in various programming languages. It abstracts the complexities of making HTTP requests and provides a unified API for developers to interact with different web services.
Features
- Support for various HTTP request methods, including GET, POST, PUT, DELETE, HEAD, PATCH
- Automatic serialization and deserialization of request/response bodies
- Ability to set custom headers, query parameters, and timeouts
- Support for handling JSON, XML, and other popular data formats
- Asynchronous and synchronous request handling
- Multipart file uploads
- Response parsing and error handling
Supported Languages
- Python
- Ruby
- Java
- PHP
- .NET
- Node.js
Getting Started
To get started with Unirest, you need to select the programming language you are using. Below, you will find the installation instructions and code examples for the supported languages:
Python
To use Unirest in Python, you can install it via pip by running the following command:
pip install unirest
Once installed, you can import Unirest and start sending HTTP requests.
Ruby
In Ruby, you can install Unirest as a gem:
gem install unirest
After installation, you can require Unirest and begin making HTTP requests.
Java
In Java, you can use Maven or Gradle to add Unirest to your project by adding the following dependency:
dependencies {
implementation 'com.konghq:unirest-java:3.11.11'
}
With the dependency added, you can import Unirest and start making HTTP requests.
PHP
In PHP, you can install Unirest using Composer. Add the following dependency to your project’s composer.json file:
{
"require": {
"mashape/unirest-php": "^3.0"
}
}
After running composer install, you can include Unirest and start making HTTP requests in PHP.
Usage Examples
Here are a few examples to demonstrate the usage of Unirest in different programming languages:
GET Request
Python:
import unirest
response = unirest.get("https://api.example.com/resource")
print(response.code) # HTTP response code
print(response.body) # Parsed response body
Ruby:
require 'unirest'
response = Unirest.get("https://api.example.com/resource")
puts response.code # HTTP response code
puts response.body # Parsed response body
Java:
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
HttpResponse<JsonNode> response = Unirest.get("https://api.example.com/resource")
.header("Accept", "application/json")
.asJson();
int statusCode = response.getStatus();
JsonNode responseBody = response.getBody();
PHP:
<?php
require_once 'vendor/autoload.php';
$response = Unirest\Request::get("https://api.example.com/resource");
echo $response->code; // HTTP response code
echo $response->body; // Parsed response body
?>
POST Request
Python:
import unirest
response = unirest.post("https://api.example.com/resource",
headers={ "Content-Type": "application/json" },
params={ "param1": "value1", "param2": "value2" })
print(response.code) # HTTP response code
print(response.body) # Parsed response body
Ruby:
require 'unirest'
response = Unirest.post("https://api.example.com/resource",
headers: { "Content-Type": "application/json" },
parameters: { param1: "value1", param2: "value2" })
puts response.code # HTTP response code
puts response.body # Parsed response body
Java:
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
HttpResponse<JsonNode> response = Unirest.post("https://api.example.com/resource")
.header("Content-Type", "application/json")
.queryString("param1", "value1")
.queryString("param2", "value2")
.asJson();
int statusCode = response.getStatus();
JsonNode responseBody = response.getBody();
PHP:
<?php
require_once 'vendor/autoload.php';
$response = Unirest\Request::post("https://api.example.com/resource",
[ "Content-Type" => "application/json" ],
json_encode([ "param1" => "value1", "param2" => "value2" ]));
echo $response->code; // HTTP response code
echo $response->body; // Parsed response body
?>
These are just a few examples to help you understand the basic usage of Unirest in different programming languages. For more detailed information, including advanced usage, error handling, and customization options, refer to the official Unirest documentation on GitHub.