Giter Club home page Giter Club logo

embeddedrest's Introduction

embeddedRest

A little library for making url requests in C++ based on unix sockets.

warning: do not use embeddedRest on iOS if you are gonna post your app to the App Store - embeddedRest doesn't have IPv6 support so your app will be rejected.

It allows to send synchronous HTTP 1.1 requests and gives pretty simple interface to specify request parameters such as

  • HTTP method
  • get parameters
  • post parameters (json)
  • port
  • HTTP headers

embeddedRest is a lightweight header-only library designed especially for mobile apps (iOS and Android) but it also can compile at any platform familiar with C++11.

GET request example

For example we need to request cities from api.vk.com.

Url = "http://api.vk.com/method/database.getCities?lang=ru&country_id=1&count=1000&need_all=1"

Code:

#include "UrlRequest.hpp"

//...

UrlRequest request;
request.host("api.vk.com");
const auto countryId=1;
const auto count=1000;
request.uri("/method/database.getCities",{
    {"lang","ru"},
    {"country_id",countryId},
    {"count",count},
    {"need_all","1"},
});
request.addHeader("Content-Type: application/json");
auto response=std::move(request.perform());
if(response.statusCode()==200){
  cout<<"status code = "<<response.statusCode()<<", body = *"<<response.body()<<"*"<<endl;
}else{
  cout<<"status code = "<<response.statusCode()<<", description = "<<response.statusDescription()<<endl;
}      

POST request example

We need to register a user and pass some arguments (login, email, password and age) to body as a json object.

UrlRequest request;
request.host("api.my-domain.com");
request.uri("/users/registration");
const auto login="Deadpool";
const auto email="[email protected]";
const auto password="dead_mf_pool123";
const auto age=24;
request.body({
    {"login",login},
    {"email",email},
    {"password",password},
    {"age",age},
});
request.method("POST");
request.addHeader("Content-Type: application/json");
auto response=std::move(request.perform());
if(response.statusCode()==200){
  cout<<"status code = "<<response.statusCode()<<", body = *"<<response.body()<<"*, user is registered!"<<endl;
}else{
  cout<<"status code = "<<response.statusCode()<<", description = "<<response.statusDescription()<<endl;
} 

Add embeddedRest to your project.

Just add "*.hpp" files from root folder into your project header directory and #include them. Also embeddedRest has dependency - rapidjson json-processor. rapidjson is also a header-only library so it is very easy to include it to your project.

Advanced

Timeout

Every UrlRequest instance has 30 seconds default timeout. If you want to change it just assign .timeout value to your request.

UrlRequest request;
request.timeout={10,0};     //  10 seconds and 0 microseconds..

timeout property has struct timeval type. This type is declared in C standard library and represents time with microseconds precision.

Passing array as get parameter

If you want to pass array to url as get parameter using square braces notation (example: jako.online/api/v1/subscribes/my?lang=ru&type[]=vk&type[]=company) you have to pass std::vector of your values to uri member function arguments. Example:

UrlRequest request;
request.host("jako.online").uri("/api/v1/subscribes/my",{
    {"lang",Language::current()},
    {"type",std::vector<std::string>{
        "vk",
        "company",
    }},
}).addHeader("Content-Type: application/json");

Request url will be parsed to jako.online/api/v1/subscribes/my?lang=ru&type[]=vk&type[]=company

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.