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.
Data has been taken from for-GET/know-your-http-well.
Variant | Scoping | Status Codes | Reason Phrases |
---|---|---|---|
C | Prefix HttpStatus_ |
enum HttpStatus_Code |
const char* |
C++ | Namespace HttpStatus |
enum Code |
std::string |
C++11 | Namespace HttpStatus |
enum class Code |
std::string |
Qt | Namespace HttpStatus |
enum Code When using Qt 5.8 or later: registered in meta type system using Q_ENUM_NS() |
QString |
#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);
}
Note: The header files contain more detailed Doxygen documentation for all types and functions.
For the complete list of defined enums, see one of the header files.
enum HttpStatus_Code
{
HttpStatus_OK = 200,
HttpStatus_NotFound = 404
// ...
};
namespace HttpStatus {
enum class Code
{
OK = 200,
Forbidden = 404
// ...
};
}
namespace HttpStatus {
enum Code
{
OK = 200,
Forbidden = 404
// ...
};
}
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.
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.
const char* HttpStatus_reasonPhrase(int code);
Returns the HTTP reason phrase string corresponding to the given code.
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.
QString HttpStatus::reasonPhrase(int code);
Returns the HTTP reason phrase string corresponding to the given code.
Note: Available only in the Qt variant and if the Qt::Network module is available.
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 matchnig "catch all" code (QNetworkReply::NoError
,
QNetworkReply::UnknownContentError
, QNetworkReply::UnknownServerError
or QNetworkReply::ProtocolFailure
)
is returned.
This project is licensed under Creative Commons CC0.