I am experimenting with some code running on an ESP8266 board to retrieve data using a web API. I would like to store the host address and key/value parameters (API key etc.) of the GET request as constants/variables (I'm not sure which is better) and then use them to compose the full GET URL.
How can I compose the full URL from separate strings/char arrays including the correct syntax for the key/values pairs?
EDIT: Just adding that I am using the ESP8266HTTPClient library.
The example I have been following uses a basic API URL with no parameters and stores the host URL as follows:
const char* host = "http://somehost.com/dir/1";
(Side question: please explain the difference between const char* host
and const char *host
if there is one)
Using this simple URL I can successfully retrieve the JSON data using this method:
HTTPClient http;
http.begin(host);
int httpCode = http.GET();
Serial.print("HTTP RESPONSE: ");
Serial.println(httpCode);
if (httpCode > 0) {
String payload = http.getString();
Serial.println("PAYLOAD:");
Serial.println(payload);
}
http.end()
I am looking into using the "ArduinoJson" library to manage the json data instead of String, but am more interested in figuring out how to compose the URL at the moment.
What I would like to achieve is adding key/value parameters stored as variables to the host URL e.g.:
const char* host = "http://somehost.com/dir/1";
char* app = "123";
char* user = "123456"
char* api_key = "XXXXXXXXXXXXX";
And then join them with strings or char arrays to create the full URL, or simply pass the host address plus the parameter values to a function that composes the URL or sends the full GET request(with the parameter "key=var", question mark, and ampersands etc.):
http://somehost.com/dir/1/?appid=123&userid=123456&key=XXXXXXXXXXXXX
This was very easy to achieve in python using the "requests" HTTP library. I was able to store the key/value parameters as a dict and then pass the host address and the parameter dict to the requests.get
function and it would compose the URL automatically.
I guess there is more than one question buried in there, but the main one is "what is the best way to compose the full URL without hard coding the whole thing?" An example would be so I could change some parameters such as appid by user input before making the GET request.
I am unsure if I am using the right approach using the character arrays, constants or variables, whether I am using the right approach by trying to compose it manually, and in general what the best practices are for doing this efficiently and avoiding memory fragmentation (which is a problem when using string variables?). This device will be required to run constantly and periodically make these GET requests.