134 lines
No EOL
12 KiB
MQL5
134 lines
No EOL
12 KiB
MQL5
//+------------------------------------------------------------------+
|
|
//| Include the file CJson class |
|
|
//+------------------------------------------------------------------+
|
|
#include "../Utils/Json.mqh"
|
|
//+------------------------------------------------------------------+
|
|
//| class : CHttpBody |
|
|
//| |
|
|
//| [PROPERTY] |
|
|
//| Name : CHttpBody |
|
|
//| Heritage : No heritage |
|
|
//| Description : Responsible for organizing and storing the body of |
|
|
//| a request. |
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
class CHttpBody
|
|
{
|
|
private:
|
|
|
|
char m_body[]; // Will store the request body as a char array
|
|
uint m_codepage; // Used to store the defined encoding
|
|
|
|
public:
|
|
CHttpBody(void);
|
|
~CHttpBody(void);
|
|
|
|
//--- Add data to the body
|
|
void AddString(string data); // Adds a text string to the request body
|
|
void AddJson(CJson &data); // Adds data in JSON format to the body
|
|
void AddBinary(char &data[]); // Allows you to add binary data
|
|
|
|
//--- Clear the body
|
|
void Clear(void); // Remove all body content
|
|
|
|
//--- Gets the body content
|
|
string ToString(void); // Returns the request body as a string
|
|
CJson ToJson(void); // Converts the request body into a JSON object, useful when the body contains structured data
|
|
void ToBinaryArray(char &body[]); // Returns the body as an array of bytes, useful for working with binary data
|
|
|
|
//--- Size in bytes
|
|
int Size(void); // Returns the size of the request body, usually in bytes
|
|
|
|
//--- Codepage
|
|
uint GetEncoding(void); // Returns the defined codepage
|
|
void SetEncoding(uint codepage); // Defines the codepage to be used
|
|
};
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
CHttpBody::CHttpBody(void)
|
|
{
|
|
m_codepage = CP_UTF8;
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
CHttpBody::~CHttpBody(void)
|
|
{
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Adds a text string to the request body |
|
|
//+------------------------------------------------------------------+
|
|
void CHttpBody::AddString(string data)
|
|
{
|
|
StringToCharArray(data,m_body,this.Size()-1,WHOLE_ARRAY,m_codepage);
|
|
ArrayRemove(m_body,this.Size()-1);
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Adds data in JSON format to the body |
|
|
//+------------------------------------------------------------------+
|
|
void CHttpBody::AddJson(CJson &data)
|
|
{
|
|
this.AddString(data.Serialize());
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Allows you to add binary data |
|
|
//+------------------------------------------------------------------+
|
|
void CHttpBody::AddBinary(char &data[])
|
|
{
|
|
ArrayCopy(m_body,data);
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Remove all body content |
|
|
//+------------------------------------------------------------------+
|
|
void CHttpBody::Clear(void)
|
|
{
|
|
ArrayFree(m_body);
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Returns the request body as a string |
|
|
//+------------------------------------------------------------------+
|
|
string CHttpBody::ToString(void)
|
|
{
|
|
return(CharArrayToString(m_body,0,WHOLE_ARRAY,m_codepage));
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Converts the request body into a JSON object, useful when the |
|
|
//| body contains structured data |
|
|
//+------------------------------------------------------------------+
|
|
CJson CHttpBody::ToJson(void)
|
|
{
|
|
CJson json;
|
|
json.Deserialize(this.ToString());
|
|
return(json);
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Returns the body as an array of bytes, useful for working with |
|
|
//| binary data |
|
|
//+------------------------------------------------------------------+
|
|
void CHttpBody::ToBinaryArray(char &body[])
|
|
{
|
|
ArrayCopy(body,m_body);
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Returns the size of the request body, usually in bytes |
|
|
//+------------------------------------------------------------------+
|
|
int CHttpBody::Size(void)
|
|
{
|
|
return(ArraySize(m_body));
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Returns the defined codepage |
|
|
//+------------------------------------------------------------------+
|
|
uint CHttpBody::GetEncoding(void)
|
|
{
|
|
return(m_codepage);
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Defines the codepage to be used |
|
|
//+------------------------------------------------------------------+
|
|
void CHttpBody::SetEncoding(uint codepage)
|
|
{
|
|
m_codepage = codepage;
|
|
}
|
|
//+------------------------------------------------------------------+ |