Defines the object which users must packed for HttpClient::send(HttpRequest*) method.
More...
#include <HttpRequest.h>
Defines the object which users must packed for HttpClient::send(HttpRequest*) method.
Please refer to tests/test-cpp/Classes/ExtensionTest/NetworkTest/HttpClientTest.cpp as a sample
- Since
- v2.0.2
@lua NA
◆ HttpRequest()
Constructor.
Because HttpRequest object will be used between UI thread and network thread, requestObj->autorelease() is forbidden to avoid crashes in AutoreleasePool new/retain/release still works, which means you need to release it manually Please refer to HttpRequestTest.cpp to find its usage.
◆ autorelease()
Override autorelease method to avoid developers to call it.
If this function was called, it would trigger assert in debug mode
- Returns
- Object* always return nullptr.
◆ setRequestType()
| void setRequestType |
( |
Type | type | ) |
|
|
inline |
◆ getRequestType()
| Type getRequestType |
( |
| ) |
const |
|
inline |
◆ setUrl()
| void setUrl |
( |
std::string_view | url | ) |
|
|
inline |
Set the url address of HttpRequest object.
The url value could be like these: "http://httpbin.org/ip" or "https://httpbin.org/get"
- Parameters
-
◆ getUrl()
| std::string_view getUrl |
( |
| ) |
const |
|
inline |
Get the url address of HttpRequest object.
- Returns
- const char* the pointer of _url.
◆ setRequestData()
| void setRequestData |
( |
const char * | buffer, |
|
|
size_t | len ) |
|
inline |
Set the request data of HttpRequest object.
- Parameters
-
| buffer | the buffer of request data, it support binary data. |
| len | the size of request data. |
◆ getRequestData()
| char * getRequestData |
( |
| ) |
|
|
inline |
Get the request data pointer of HttpRequest object.
- Returns
- char* the request data pointer.
◆ getRequestDataSize()
| ssize_t getRequestDataSize |
( |
| ) |
const |
|
inline |
Get the size of request data.
- Returns
- ssize_t the size of request data
◆ setTag()
| void setTag |
( |
std::string_view | tag | ) |
|
|
inline |
Set a string tag to identify your request.
This tag can be found in HttpResponse->getHttpRequest->getTag().
- Parameters
-
◆ getTag()
| std::string_view getTag |
( |
| ) |
const |
|
inline |
Get the string tag to identify the request.
The best practice is to use it in your MyClass::onMyHttpRequestCompleted(sender, HttpResponse*) callback.
- Returns
- const char* the pointer of _tag
◆ setUserData()
| void setUserData |
( |
void * | pUserData | ) |
|
|
inline |
Set user-customed data of HttpRequest object.
You can attach a customed data in each request, and get it back in response callback. But you need to new/delete the data pointer manually.
- Parameters
-
| pUserData | the string pointer |
◆ getUserData()
| void * getUserData |
( |
| ) |
const |
|
inline |
Get the user-customed data pointer which were pre-setted.
Don't forget to delete it. HttpClient/HttpResponse/HttpRequest will do nothing with this pointer.
- Returns
- void* the pointer of user-customed data.
◆ setCompleteCallback()
| void setCompleteCallback |
( |
const HttpCompleteCallback & | callback | ) |
|
|
inline |
Set response callback function of HttpRequest object.
When response come back, we would call _pCallback to process response data.
- Parameters
-
| callback | the HttpCompleteCallback function. |
◆ getCompleteCallback()
| const HttpCompleteCallback & getCompleteCallback |
( |
| ) |
const |
|
inline |
Get HttpCompleteCallback callback function.
- Returns
- const HttpCompleteCallback& completeCallback callback function.
◆ setDataCallback()
| void setDataCallback |
( |
const HttpDataCallback & | callback | ) |
|
|
inline |
Set the data callback for handling received data chunks.
When a data callback is set, it will be invoked for each chunk of data received during the HTTP response. This is particularly useful for:
- Large file downloads to process data incrementally
- Streaming data processing
- Memory-constrained environments
- Note
- If a data callback is set, the default behavior of appending received data to HttpResponse::_responseData will be disabled. The response data buffer will not accumulate data chunks automatically. You must handle all data storage/processing within the callback function.
-
The callback receives the following parameters:
- HttpResponse* response: The response object containing request context
- const char* data: Pointer to the received data chunk
- size_t data_size: Size of the data chunk in bytes
- Parameters
-
| callback | The callback function to handle received data chunks |
request->setDataCallback([](
HttpResponse* response,
const char* data,
size_t size) {
auto expectedTotal = response->getContentLength();
fwrite(data, 1, size, outputFile);
totalReceived += size;
updateProgress(totalReceived, expectedTotal);
});
defines the object which users will receive at onHttpCompleted(sender, HttpResponse) callback.
Definition HttpResponse.h:55
- See also
- HttpDataCallback
-
HttpResponse::_responseData
◆ setHeaders()
| void setHeaders |
( |
const std::vector< std::string > & | headers | ) |
|
|
inline |
Set custom-defined headers.
- Parameters
-
| headers | The string vector of custom-defined headers. |
◆ getHeaders()
| const std::vector< std::string > & getHeaders |
( |
| ) |
const |
|
inline |
Get custom headers.
- Returns
- std::vector<std::string> the string vector of custom-defined headers.
The documentation for this class was generated from the following file: