• Stars
    star
    798
  • Rank 57,078 (Top 2 %)
  • Language
    C++
  • License
    The Unlicense
  • Created over 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Single-header C++ HTTP request class

HTTPRequest

HTTPRequest is a single-header C++ library for making HTTP requests. You can just include it in your project and use it. HTTPRequest was tested on macOS, Windows, Haiku, BSD, and GNU/Linux, but it should work on most of the Linux-based platforms. Supports IPv4 and IPv6. HTTRequest requires C++17 or newer.

Usage

To use the library simply include HTTPRequest.hpp using #include "HTTPRequest.hpp".

Example of a GET request

try
{
    // you can pass http::InternetProtocol::V6 to Request to make an IPv6 request
    http::Request request{"http://test.com/test"};

    // send a get request
    const auto response = request.send("GET");
    std::cout << std::string{response.body.begin(), response.body.end()} << '\n'; // print the result
}
catch (const std::exception& e)
{
    std::cerr << "Request failed, error: " << e.what() << '\n';
}

Example of a POST request with form data

try
{
    http::Request request{"http://test.com/test"};
    const string body = "foo=1&bar=baz";
    const auto response = request.send("POST", body, {
        {"Content-Type", "application/x-www-form-urlencoded"}
    });
    std::cout << std::string{response.body.begin(), response.body.end()} << '\n'; // print the result
}
catch (const std::exception& e)
{
    std::cerr << "Request failed, error: " << e.what() << '\n';
}

Example of a POST request with a JSON body

try
{
    http::Request request{"http://test.com/test"};
    const std::string body = "{\"foo\": 1, \"bar\": \"baz\"}";
    const auto response = request.send("POST", body, {
        {"Content-Type", "application/json"}
    });
    std::cout << std::string{response.body.begin(), response.body.end()} << '\n'; // print the result
}
catch (const std::exception& e)
{
    std::cerr << "Request failed, error: " << e.what() << '\n';
}

Example of a GET request using Basic authorization

try
{
    http::Request request{"http://user:[email protected]/test"};
    const auto response = request.send("GET");
    std::cout << std::string{response.body.begin(), response.body.end()} << '\n'; // print the result
}
catch (const std::exception& e)
{
    std::cerr << "Request failed, error: " << e.what() << '\n';
}

To set a timeout for HTTP requests, pass std::chrono::duration as a last parameter to send(). A negative duration (default) passed to send() disables timeout.

License

HTTPRequest is released to the Public Domain.

More Repositories

1

ouzel

C++ game engine for Windows, macOS, Linux, iOS, tvOS, Android, and web browsers
C++
965
star
2

rtmp_relay

RTMP relay
C++
61
star
3

volkswagencpp

Volkswagen C++ makes your tests pass when run on a CI server
C++
55
star
4

contenttype

Go library for HTTP content type negotiation
Go
43
star
5

SoftwareRenderer

Simple header-only C++ software renderer
C++
31
star
6

RealisticWaterSceneNode

Realistic water scene node for Irrlicht
C++
24
star
7

signature_check

Mach-O signature check
C
23
star
8

cppsocket

C++ socket wrapper
C++
18
star
9

hlibs

Header-only C++ libraries
C++
14
star
10

bin2h

Store binary file in C header
C
12
star
11

self

Proposal for a self keyword for C++
8
star
12

OuzelShadingLanguage

Ouzel Shading Language is a high-level language, designed for writing cross-platform shaders
C++
5
star
13

gamepads

Gamepad configurations
C
4
star
14

FlurryX

C++ wrapper for Flurry Analytics for Cocos2d-x
C++
4
star
15

ouzel_spine

Ouzel Spine import
C++
3
star
16

smart_ptr

Custom smart pointer implementation
C++
3
star
17

json

C++
2
star
18

NoPreprocessor

C++
2
star
19

easing

Header-only C++ easing library
C++
2
star
20

CCShake

Shake action for cocos2d-x
C++
2
star
21

tvos-test

cocos2d-x test with tvOS
Objective-C++
2
star
22

Xcode-shortcuts

Xcode shortcuts
1
star
23

chat

C++
1
star
24

omath

C++
1
star
25

oc

C
1
star
26

EmptyWindow

Objective-C++
1
star
27

websocket

C++
1
star
28

carddetection

Playing card detection
C++
1
star
29

rebellion

Utilities for Star Wars: Rebellion board game
HTML
1
star
30

ecs

C++
1
star
31

video_play

Video player using Ouzel engine and libav
C++
1
star
32

metalpp

Header-only C++ Metal wrapper
C++
1
star
33

pcmplayer

C++
1
star
34

ini

C++
1
star
35

FileStdio

C++
1
star
36

nginx-screenshot-module

C
1
star
37

xml

C++
1
star