Home

Awesome

HTTP Status Codes and Reason Phrases for C, C++ and Qt

Linux build status Windows build status

This repository provides the HTTP status codes and reason phrases in different variants for C/C++.

Simply take the variant that fits your needs and copy/use/modify it.

The status codes cover all codes registered with the IANA. Initially, the data was taken from for-GET/know-your-http-well.

Table of Contents

Variants

VariantName ScopingStatus Codes TypeReason Phrases Type
CPrefix HttpStatus_enum HttpStatus_Codeconst char*
C++Namespace HttpStatusenum Codestd::string
C++11Namespace HttpStatusenum class Codestd::string
QtNamespace HttpStatusenum Code<br>When using Qt 5.8 or later: registered in meta type system using Q_ENUM_NS()QString

Note regarding Qt variant: Oldest tested Qt version was Qt 5.2.0 with MinGW 4.8. However, should be working with any Qt 5.x version. Might also be working with Qt 4 versions but this has not been tested.

Example

#include "MyHttpReplyClass.h"
#include "HttpStatusCodes_C++.h"
#include <iostream>

void printReplyStatus( MyHttpReplyClass reply )
{
	if ( reply.status == HttpStatus::OK )
		std::cout << "Success!";
	else
		std::cerr << reply.status << " " << HttpStatus::reasonPhrase( reply.status );
}

Documentation

Note: The header files contain more detailed Doxygen documentation for all types and functions.

Status Codes Enum

For the complete list of defined enums, see one of the header files.

Note: The maximum supported value for the enums is 1023. Trying to convert bigger values to the enum types might be undefined behavior.

C Variant
enum HttpStatus_Code
{
	HttpStatus_OK       = 200,
	HttpStatus_NotFound = 404
	// ...
};
C++11 Variant
namespace HttpStatus {
enum class Code
{
	OK       = 200,
	NotFound = 404
	// ...
};
}
Other Variants
namespace HttpStatus {
enum Code
{
	OK       = 200,
	NotFound = 404
	// ...
};
}

Category/Class Tests

C Variant
char HttpStatus_isInformational( int code );
char HttpStatus_isSuccessful( int code );
char HttpStatus_isRedirection( int code );
char HttpStatus_isClientError( int code );
char HttpStatus_isServerError( int code );

Return 1 if the given code belongs to the corresponding class of status codes (see RFC7231). Return 0 otherwise.

char HttpStatus_isError( int code);

Returns 1 if the given code is either a client error, a server error or any non-standard error code. Non-standard error codes are status codes with a value of 600 or higher. Returns 0 otherwise.

Other Variants

Note: The C++11 variant also provides overloads for HttpStatus::Code. So there is no need to cast.

bool HttpStatus::isInformational( int code );
bool HttpStatus::isSuccessful( int code );
bool HttpStatus::isRedirection( int code );
bool HttpStatus::isClientError( int code );
bool HttpStatus::isServerError( int code );

Return true if the given code belongs to the corresponding class of status codes (see RFC7231). Return false otherwise.

bool HttpStatus::isError( int code );

Returns true if the given code is either a client error, a server error or any non-standard error code. Non-standard error codes are status codes with a value of 600 or higher. Returns false otherwise.

Reason Phrases

C Variant
const char* HttpStatus_reasonPhrase( int code );

Returns the HTTP reason phrase string corresponding to the given code.

C++/C++11 Variants

Note: The C++11 variant also provides an overload for HttpStatus::Code. So there is no need to cast.

std::string HttpStatus::reasonPhrase( int code );

Returns the HTTP reason phrase string corresponding to the given code.

Qt Variant
QString HttpStatus::reasonPhrase( int code );

Returns the HTTP reason phrase string corresponding to the given code.

Conversion Functions

C++11 Variant
int HttpStatus::toInt( HttpStatus::Code code );

Returns the integer value corresponding to a given a code. This is a convenience function as replacement for a static_cast<int>().

Qt Variant
int HttpStatus::networkErrorToStatusCode( QNetworkReply::NetworkError error );

Returns the HTTP status code corresponding to the given error if there is one. Otherwise, -1 is returned.

QNetworkReply::NetworkError HttpStatus::statusCodeToNetworkError( int code );

Returns the QNetworkReply::NetworkError corresponding to the given code if there is one. For codes where there is no exact match, the best matching "catch all" code (QNetworkReply::NoError, QNetworkReply::UnknownContentError, QNetworkReply::UnknownServerError or QNetworkReply::ProtocolFailure) is returned.

License

CC0 This project is licensed under Creative Commons CC0.