drogon
C++14/17-based HTTP application framework
Loading...
Searching...
No Matches
drogon::HttpRequest Class Referenceabstract

Abstract class for webapp developer to get or set the Http request;. More...

#include <drogon/HttpRequest.h>

Public Member Functions

template<typename T>
 operator T () const
 This template enables implicit type conversion. For using this template, user must specialize the fromRequest template. For example a shared_ptr<Json::Value> specialization version is available above, so we can use the following code to get a json object:
template<typename T>
as () const
 This template enables explicit type conversion, see the above template.
virtual const char * methodString () const =0
 Return the method string of the request, such as GET, POST, etc.
const char * getMethodString () const
virtual HttpMethod method () const =0
 Return the enum type method of the request.
HttpMethod getMethod () const
virtual bool isHead () const =0
 Check if the method is or was HttpMethod::Head.
virtual const std::string & getHeader (std::string key) const =0
 Get the header string identified by the key parameter.
virtual void addHeader (std::string field, const std::string &value)=0
 Set the header string identified by the field parameter.
virtual void addHeader (std::string field, std::string &&value)=0
virtual void removeHeader (std::string key)=0
 Remove the header identified by the key parameter.
virtual void clearHeaders ()=0
virtual const std::string & getCookie (const std::string &field) const =0
 Get the cookie string identified by the field parameter.
virtual const SafeStringMap< std::string > & headers () const =0
 Get all headers of the request.
const SafeStringMap< std::string > & getHeaders () const
 Get all headers of the request.
virtual const SafeStringMap< std::string > & cookies () const =0
 Get all cookies of the request.
const SafeStringMap< std::string > & getCookies () const
 Get all cookies of the request.
virtual size_t realContentLength () const =0
 Return content length parsed from the Content-Length header If no Content-Length header, return null.
size_t getRealContentLength () const
virtual const std::string & query () const =0
 Get the query string of the request.
const std::string & getQuery () const
 Get the query string of the request.
std::string_view body () const
std::string_view getBody () const
virtual const char * bodyData () const =0
virtual size_t bodyLength () const =0
virtual void setBody (const std::string &body)=0
 Set the content string of the request.
virtual void setBody (std::string &&body)=0
 Set the content string of the request.
virtual const std::string & path () const =0
 Get the path of the request.
virtual const std::string & getOriginalPath () const =0
 Get the original path of the request.(before url-decoding).
const std::string & getPath () const
 Get the path of the request.
std::string_view getMatchedPathPattern () const
 Get the matched path pattern after routing.
std::string_view matchedPathPattern () const
 Get the matched path pattern after routing.
virtual const std::vector< std::string > & getRoutingParameters () const =0
virtual void setRoutingParameters (std::vector< std::string > &&params)=0
 This method usually is called by the framework.
virtual const char * matchedPathPatternData () const =0
virtual size_t matchedPathPatternLength () const =0
virtual const char * versionString () const =0
const char * getVersionString () const
virtual Version version () const =0
 Return the enum type version of the request.
Version getVersion () const
 Return the enum type version of the request.
virtual const SessionPtr & session () const =0
 Get the session to which the request belongs.
const SessionPtr & getSession () const
 Get the session to which the request belongs.
virtual const AttributesPtr & attributes () const =0
const AttributesPtr & getAttributes () const
virtual const SafeStringMap< std::string > & parameters () const =0
 Get parameters of the request.
const SafeStringMap< std::string > & getParameters () const
 Get parameters of the request.
virtual const std::string & getParameter (const std::string &key) const =0
 Get a parameter identified by the.
template<typename T>
std::optional< T > getOptionalParameter (const std::string &key)
 Get the optional parameter identified by the key. if the parameter doesn't exist, or the original parameter can't be converted to a T type object, an empty optional object is returned.
virtual const trantor::InetAddress & peerAddr () const =0
 Return the remote IP address and port.
const trantor::InetAddress & getPeerAddr () const
virtual const trantor::InetAddress & localAddr () const =0
 Return the local IP address and port.
const trantor::InetAddress & getLocalAddr () const
virtual const trantor::Date & creationDate () const =0
 Return the creation timestamp set by the framework.
const trantor::Date & getCreationDate () const
virtual const trantor::CertificatePtr & peerCertificate () const =0
const trantor::CertificatePtr & getPeerCertificate () const
virtual const std::shared_ptr< Json::Value > & jsonObject () const =0
 Get the Json object of the request.
const std::shared_ptr< Json::Value > & getJsonObject () const
 Get the Json object of the request.
virtual const std::string & getJsonError () const =0
 Get the error message of parsing the JSON body received from peer. This method usually is called after getting a empty shared_ptr object by the getJsonObject() method.
virtual ContentType contentType () const =0
 Get the content type.
ContentType getContentType () const
virtual void setMethod (const HttpMethod method)=0
 Set the Http method.
virtual void setPath (const std::string &path)=0
virtual void setPath (std::string &&path)=0
virtual void setPathEncode (bool)=0
 The default behavior is to encode the value of setPath using urlEncode. Setting the path encode to false avoid the value of path will be changed by the library.
virtual void setParameter (const std::string &key, const std::string &value)=0
 Set the parameter of the request.
virtual void setQueryParameter (const std::string &key, const std::string &value)=0
virtual void setBodyParameter (const std::string &key, const std::string &value)=0
virtual void setContentTypeCode (const ContentType type)=0
 Set or get the content type.
void setContentTypeString (const std::string_view &typeString)
 For example, "content-type: text/plain\r\n" or "text/plain".
virtual void setCustomContentTypeString (const std::string &type)=0
virtual void addCookie (std::string key, std::string value)=0
 Add a cookie.
virtual void setPassThrough (bool flag)=0
 Set the request object to the pass-through mode or not. It's not by default when a new request object is created. In pass-through mode, no additional headers (including user-agent, connection, etc.) are added to the request. This mode is useful for some applications such as a proxy.
bool isCorsRequest () const
 Check if the request is a CORS request.
bool isCorsPreflightRequest () const
 Check if the request is a CORS pre-flight request.
virtual bool isOnSecureConnection () const noexcept=0
virtual void setContentTypeString (const char *typeString, size_t typeStringLength)=0
virtual bool connected () const noexcept=0
virtual const std::weak_ptr< trantor::TcpConnection > & getConnectionPtr () const noexcept=0

Static Public Member Functions

static HttpRequestPtr newHttpRequest ()
 Create a normal request with http method Get and version Http1.1.
static HttpRequestPtr newHttpJsonRequest (const Json::Value &data)
static HttpRequestPtr newHttpFormPostRequest ()
static HttpRequestPtr newFileUploadRequest (const std::vector< UploadFile > &files)
template<typename T>
static HttpRequestPtr newCustomHttpRequest (T &&obj)
 Create a custom HTTP request object. For using this template, users must specialize the toRequest template.

Detailed Description

Abstract class for webapp developer to get or set the Http request;.

Member Function Documentation

◆ addHeader()

virtual void drogon::HttpRequest::addHeader ( std::string field,
const std::string & value )
pure virtual

Set the header string identified by the field parameter.

Parameters
fieldThe field parameter is transformed to lower case before storing.
valueThe value of the header.

◆ attributes()

virtual const AttributesPtr & drogon::HttpRequest::attributes ( ) const
pure virtual

Get the attributes store, users can add/get any type of data to/from this store

◆ body()

std::string_view drogon::HttpRequest::body ( ) const
inline

Get the content string of the request, which is the body part of the request.

◆ getAttributes()

const AttributesPtr & drogon::HttpRequest::getAttributes ( ) const
inline

Get the attributes store, users can add/get any type of data to/from this store

◆ getBody()

std::string_view drogon::HttpRequest::getBody ( ) const
inline

Get the content string of the request, which is the body part of the request.

◆ getHeader()

virtual const std::string & drogon::HttpRequest::getHeader ( std::string key) const
pure virtual

Get the header string identified by the key parameter.

Note
If there is no the header, a empty string is returned. The key is case insensitive

◆ getJsonError()

virtual const std::string & drogon::HttpRequest::getJsonError ( ) const
pure virtual

Get the error message of parsing the JSON body received from peer. This method usually is called after getting a empty shared_ptr object by the getJsonObject() method.

Returns
const std::string& The error message. An empty string is returned when no error occurs.

◆ getOptionalParameter()

template<typename T>
std::optional< T > drogon::HttpRequest::getOptionalParameter ( const std::string & key)
inline

Get the optional parameter identified by the key. if the parameter doesn't exist, or the original parameter can't be converted to a T type object, an empty optional object is returned.

Template Parameters
T
Parameters
key
Returns
optional<T>

◆ getParameter()

virtual const std::string & drogon::HttpRequest::getParameter ( const std::string & key) const
pure virtual

Get a parameter identified by the.

Parameters
key

◆ getRoutingParameters()

virtual const std::vector< std::string > & drogon::HttpRequest::getRoutingParameters ( ) const
pure virtual

Get the matched path pattern after routing (including matched parameters in the query string)

◆ isCorsPreflightRequest()

bool drogon::HttpRequest::isCorsPreflightRequest ( ) const
inline

Check if the request is a CORS pre-flight request.

Check if the method of the request is OPTIONS and if it is a CORS pre-flight request.
It should contain:

  • Origin: origination page
  • Access-Control-Request-Method: method to be used in the actual request
    Returns
    true if the method is OPTIONS and the required CORS pre-flight headers are present

◆ isCorsRequest()

bool drogon::HttpRequest::isCorsRequest ( ) const
inline

Check if the request is a CORS request.

It should contain:

  • Origin: origination page
    Returns
    true if the Origin header is present

◆ isHead()

virtual bool drogon::HttpRequest::isHead ( ) const
pure virtual

Check if the method is or was HttpMethod::Head.

Allows to know that an incoming request is a HEAD request, since drogon sets the method to HttpMethod::Get before calling the controller

Returns
true if method() returns HttpMethod::Head, or HttpMethod::Get but was previously HttpMethod::Head

◆ jsonObject()

virtual const std::shared_ptr< Json::Value > & drogon::HttpRequest::jsonObject ( ) const
pure virtual

Get the Json object of the request.

The content type of the request must be 'application/json', otherwise the method returns an empty shared_ptr object.

◆ newFileUploadRequest()

HttpRequestPtr drogon::HttpRequest::newFileUploadRequest ( const std::vector< UploadFile > & files)
static

Create a http file upload request with: Method: Post Version: Http1.1 Content type: multipart/form-data The

Parameters
filesrepresents pload files which are transferred to the server via the multipart/form-data format

◆ newHttpFormPostRequest()

HttpRequestPtr drogon::HttpRequest::newHttpFormPostRequest ( )
static

Create a http request with: Method: Post Version: Http1.1 Content type: application/x-www-form-urlencoded

◆ newHttpJsonRequest()

HttpRequestPtr drogon::HttpRequest::newHttpJsonRequest ( const Json::Value & data)
static

Create a http request with: Method: Get Version: Http1.1 Content type: application/json, the

Parameters
datais serialized into the content of the request.

◆ newHttpRequest()

HttpRequestPtr drogon::HttpRequest::newHttpRequest ( )
static

Create a normal request with http method Get and version Http1.1.

The following methods are a series of factory methods that help users create request objects.

◆ operator T()

template<typename T>
drogon::HttpRequest::operator T ( ) const
inline

This template enables implicit type conversion. For using this template, user must specialize the fromRequest template. For example a shared_ptr<Json::Value> specialization version is available above, so we can use the following code to get a json object:

std::shared_ptr<Json::Value> jsonPtr = *requestPtr;

With this template, user can use their favorite JSON library instead of the default jsoncpp library or convert the request to an object of any custom type.

◆ query()

virtual const std::string & drogon::HttpRequest::query ( ) const
pure virtual

Get the query string of the request.

The query string is the substring after the '?' in the URL string.

◆ removeHeader()

virtual void drogon::HttpRequest::removeHeader ( std::string key)
pure virtual

Remove the header identified by the key parameter.

Parameters
keyThe key is case insensitive

◆ setBodyParameter()

virtual void drogon::HttpRequest::setBodyParameter ( const std::string & key,
const std::string & value )
pure virtual

Set the parameter to the request body.

Warning
The content type must be application/x-www-form-urlencoded or multipart/form-data

◆ setContentTypeString()

void drogon::HttpRequest::setContentTypeString ( const std::string_view & typeString)
inline

For example, "content-type: text/plain\r\n" or "text/plain".

Set the content-type string, The string may contain the header name and CRLF. Or just the MIME type

◆ setCustomContentTypeString()

virtual void drogon::HttpRequest::setCustomContentTypeString ( const std::string & type)
pure virtual

Set the request content-type string, The string must contain the header name and CRLF. For example, "content-type: text/plain\r\n"

◆ setPassThrough()

virtual void drogon::HttpRequest::setPassThrough ( bool flag)
pure virtual

Set the request object to the pass-through mode or not. It's not by default when a new request object is created. In pass-through mode, no additional headers (including user-agent, connection, etc.) are added to the request. This mode is useful for some applications such as a proxy.

Parameters
flag

◆ setPath()

virtual void drogon::HttpRequest::setPath ( const std::string & path)
pure virtual

Set the path of the request

Note
The path is automatically encoded. use setPathEncode(false) to avoid this.

◆ setPathEncode()

virtual void drogon::HttpRequest::setPathEncode ( bool )
pure virtual

The default behavior is to encode the value of setPath using urlEncode. Setting the path encode to false avoid the value of path will be changed by the library.

Parameters
booltrue --> the path will be url encoded false --> using value of path as it is set

◆ setQueryParameter()

virtual void drogon::HttpRequest::setQueryParameter ( const std::string & key,
const std::string & value )
pure virtual

Set the parameter to the query, regardless of the HTTP method or content type

◆ version()

virtual Version drogon::HttpRequest::version ( ) const
pure virtual

Return the enum type version of the request.

kHttp10 means Http version is 1.0 kHttp11 means Http version is 1.1

◆ versionString()

virtual const char * drogon::HttpRequest::versionString ( ) const
pure virtual

Return the string of http version of request, such as HTTP/1.0, HTTP/1.1, etc.


The documentation for this class was generated from the following file: