2

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.

1 Answer 1

0

Side question: please explain the difference between const char* host and const char *host if there is one

There is no difference; n.b. the arduino IDE uses C++! If you don't know C or C++ you should learn that first.

"what is the best way to compose the full URL without hard coding the whole thing?"

This is basically the same question as how do you manipulate, i.e. compose, composite, concatenate, C strings. c string functions

here is a quick example, without error checking, etc.:

const char* host = "http://somehost.com/dir/1";
char* app = "123";
char* user = "123456"
char* api_key = "XXXXXXXXXXXXX";

char urlout[255];

// we are going to start here
strcpy(urlout, host);
strcat(urlout, "/?appid=");
strcat(urlout, app);
strcat(urlout, "&userid=");
strcat(urlout, user);
strcat(urlout, "&key=");
strcat(urlout, api_key);

// urlout == "http://somehost.com/dir/1/?appid=123&userid=123456&key=XXXXXXXXXXXXX"
2
  • Thank you, your solution is what I was looking for. However I don't know if "learn c++ first" was very constructive. Arduino is meant to be standalone, for education, and is many people's first intro into programming. I also just thought I would add in a link to an article that explained in depth and helped me understand about strings, character arrays, and managing memory usage effectively. The Evils of Arduino Strings
    – justinjt
    May 16, 2019 at 3:27
  • @justincs I think the idea that Arduino is meant to be standalone is problematic; the language used is C++, one needs to know enough C++ to use the Arduino, hiding that fact or pretending it doesn't matter/exist does a disservice to the users. It just makes things more confusing; there is simplification, and there is oversimplification! Users now have two problems: their original problem, and learning to program at the same time; this does more harm than good IMHO.
    – esoterik
    May 16, 2019 at 23:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.