-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
ENG 09 0 References request
The HttpRequest type pointer commonly named req
in the examples in this documentation represents the data contained in a request received or sent by drogon, below are the some methods by which you can interact with this object:
-
Function that returns if the request was made on https.
None.
bool type.
-
Function that returns the request method. Useful to differentiate the request method if a single handle allows more than one type.
None.
HttpMethod
request method object.#include "mycontroller.h" using namespace drogon; void mycontroller::anyhandle(const HttpRequestPtr &req, std::function<void (const HttpResponsePtr &)> &&callback) { if (req->getMethod() == HttpMethod::Get) { // do something } else if (req->getMethod() == HttpMethod::Post) { // do other something } }
-
Function that returns the value of a parameter based on an identifier. The behavior changes based on the Get or Post request type.
string type identifier of param.
param content on
string
format.On Get type:
#include "mycontroller.h" #include <string> using namespace drogon; void mycontroller::anyhandle(const HttpRequestPtr &req, std::function<void (const HttpResponsePtr &)> &&callback) { // https://mysite.com/an-path/?id=5 std::string id = req->getParameter("id"); // or long id = std::strtol(req->getParameter("id")); }
Or On Post type:
#include "mycontroller.h" #include <string> using namespace drogon; void mycontroller::loginHandle(const HttpRequestPtr &req, std::function<void (const HttpResponsePtr &)> &&callback) { // request contain a Form Login std::string email = req->getParameter("email"); std::string password = req->getParameter("password"); }
-
path()
Function that returns the request path. Useful if you use an ADD_METHOD_VIA_REGEX or other type of dynamic URL in the controller.
None.
string representing the request path.
#include "mycontroller.h" #include <string> using namespace drogon; void mycontroller::anyhandle(const HttpRequestPtr &req, std::function<void (const HttpResponsePtr &)> &&callback) { // https://mysite.com/an-path/?id=5 std::string url = req->getPath(); // url = /an-path/ }
-
body()
Function that returns the request body content (if any).
None.
String representing the request body (if any).
-
Function that returns a request header based on an identifier.
String header identifier.
The content of the header in string format.
#include "mycontroller.h" #include <string> using namespace drogon; void mycontroller::anyhandle(const HttpRequestPtr &req, std::function<void (const HttpResponsePtr &)> &&callback) { if (req->getHeader("Host") != "mysite.com") { // return http 403 } }
-
Function that returns all headers of a request.
None.
An unordered_map containing the headers.
#include "mycontroller.h" #include <unordered_map> #include <string> using namespace drogon; void mycontroller::anyhandle(const HttpRequestPtr &req, std::function<void (const HttpResponsePtr &)> &&callback) { for (const std::pair<const std::string, const std::string> &header : req->headers()) { auto header_key = header.first; auto header_value = header.second; } }
-
Function that returns request cookie based on an identifier.
None.
Value of cookie on string format.
-
Function that returns all cookies of a request.
None.
An unordered_map containing the cookies.
#include "mycontroller.h" #include <unordered_map> #include <string> using namespace drogon; void mycontroller::anyhandle(const HttpRequestPtr &req, std::function<void (const HttpResponsePtr &)> &&callback) { for (const std::pair<const std::string, const std::string> &header : req->cookies()) { auto cookie_key = header.first; auto cookie_value = header.second; } }
-
Function that converts the body value of a request into a Json object (normally POST requests).
None.
A Json object.
#include "mycontroller.h" using namespace drogon; void mycontroller::anyhandle(const HttpRequestPtr &req, std::function<void (const HttpResponsePtr &)> &&callback) { // body = {"email": "test@gmail.com"} auto jsonData = *req->getJsonObject(); std::string email = jsonData["email"].asString(); }
From here are not methods of the Http Request object, but some useful things you can do to process the requests you will receive
#include "mycontroller.h"
using namespace drogon;
void mycontroller::postfile(const HttpRequestPtr &req, std::function<void (const HttpResponsePtr &)> &&callback) {
// Only Post Requests (File Form)
MultiPartParser file;
file.parse(req);
if (file.getFiles().empty()) {
// Not Files Found
}
// Get First file and save then
const HttpFile archive = file.getFiles()[0];
archive.saveAs("/tmp/" + archive.getFileName());
}
For more information about parsing file: File Handler
Next: Plugins
- Overview
- Install drogon
- Quick Start
- Controller
- Middleware and Filter
- View
- Session
- Database
- References
- Plugins
- Configuration File
- drogon_ctl Command
- AOP
- Benchmarks
- Coz profiling
- Brotli info
- Coroutines
- Redis
- Testing Framework
- FAQ