GithubHelp home page GithubHelp logo

harukihosono / mql_requests Goto Github PK

View Code? Open in Web Editor NEW

This project forked from vivazzi/mql_requests

0.0 0.0 0.0 30 KB

Requests is a simple HTTP library for mql4, built for human beings

License: MIT License

MQL4 21.52% MQL5 78.48%

mql_requests's Introduction

Requests

Requests is a simple HTTP library for mql4, built for human beings. Requests lib allows you to send HTTP/1.1 requests easily.

Installing

Download repo and copy mql_requests/Include/requests.mqh folder to <TERMINAL DIR>/MQL(4/5)/Include

Simple usage

#include <requests/requests.mqh>

int OnInit(){
    ...
    
    Requests requests;
    Response response = requests.get("https://site.com/some/url?par=foo&par_2=bar");
    Print("Response: " + response.text);
    // Response: some_response_of_url

    Print("Response parameters: " + response.parameters);
    // Response query: par=foo&par_2=bar
}

You can send GET or POST requests:

response = requests.get("https://site.com/some/url?par=foo&par_2=bar");
response = requests.post("https://site.com/some/url", "par=foo&par_2=bar");

// or send() for dynamic define request query
string method = "POST";
response = requests.send(method, "https://site.com/some/url", "par=foo&par_2=bar");

Using requests.get(), you can use GET-parameter in url and data together. Next examples are equivalent:

response = requests.get("https://site.com/some/url?par=foo&par_2=bar");
response = requests.get("https://site.com/some/url", "par=foo&par_2=bar");
response = requests.get("https://site.com/some/url?par=foo", "par_2=bar");
Print("Response parameters: " + response.parameters);
// "Response parameters: par=foo&par_2=bar"

You can use array of string for request data:

string array_data[2][2];
array_data[0][0] = "par_1"; array_data[0][1] = "foo";
array_data[1][0] = "par_2"; array_data[1][1] = "bar";

response = requests.get(url, array_data);

Usage with RequestData

RequestData request_data;
request_data.add("par", "foo");

Requests requests;
Response response = requests.get("https://site.com/some/url", request_data);
Print("Response: " + response.text);
// Response: some_response_of_url

Print("Response parameters: " + response.parameters);
// Response parameters: par=foo

Features

  • HTTP connection reuse
  • Sending of GET or POST requests

Detailed information of using Requests

You can define DEBUG_REQUESTS for display more detailed information of usage Requests:

#include "lib/requests.mqh"

#define DEBUG_REQUESTS


int OnInit(){
    Requests requests;
    Response response = requests.get("https://site.com/some/url", "par=foo&par_2=bar");
    Print("Response: " + response.text);
}

With DEBUG_REQUESTS you will an addition information to journal Terminal/Experts.

API RequestData

RequestData is helper class for simple create request data.

USAGE:

RequestData request_data;
request_data.add("par_1", "foo");
request_data.add("par_2", "bar");

Requests requests;
Response response = requests.get(url, request_data);
Print("Response: " + response.text);

You can replace value of pair using the same name in request_data.add():

RequestData request_data;
request_data.add("par_1", "foo");
request_data.add("par_2", "bar");

Print(request_data.to_str());
// "par_1=foo&par_2=bar"

request_data.add("par_2", "super_bar");
Print(request_data.to_str());
// "par_1=foo&par_2=super_bar"

Use request_data.remove() for clear data and fill request_data new data:

RequestData request_data;
request_data.add("par_1", "foo");
request_data.add("par_2", "bar");
request_data.add("par_3", "baz");
Print(request_data.to_str());
// "par_1=foo&par_2=bar&par_3=baz"

request_data.remove("par_2");  // removes data pair with specific name
Print(request_data.to_str());
// "par_1=foo&par_3=baz"

request_data.remove();  // removes all data pairs
Print(request_data.to_str());
// ""

Use static method to_str(string& _data[][]) if you have an array of pairs:

string array_data[2][2];
array_data[0][0] = "par_1"; array_data[0][1] = "foo";
array_data[1][0] = "par_2"; array_data[1][1] = "bar";
Print(RequestData::to_str(array_data));
// "par_1=foo&par_2=bar&par_3=baz"

Run tests

  1. Copy mql_requests/Experts/TestRequest.mq4 to <TERMINAL DIR>/MQL(4/5)/Experts
  2. Download mql_unit_test and copy mql_unit_test/Include/unit_test.mqh folder to <TERMINAL DIR>/MQL(4/5)/Include
  3. Compile TestRequest.mq4 and run TestRequest.ex4 in terminal in a window of any trading pair.
  4. Look test result in <TERMINAL DIR>/Files/TestRequests_unit_test_log.txt

CONTRIBUTING

To reporting bugs or suggest improvements, please use the issue tracker.

Thank you very much, that you would like to contribute to mql_requests. Thanks to the present, past and future contributors.

If you think you have discovered a security issue in our code, please do not create issue or raise it in any public forum until we have had a chance to deal with it. For security issues use [email protected]

LINKS

LICENCE

Copyright © 2022 Artem Maltsev and contributors.

MIT licensed.

mql_requests's People

Contributors

harukihosono avatar vivazzi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.