Formatting long URL for php POST with many inputs. What is the right approach?

I’m trying to send some data via http POST to my sql server. There are numerous datapoints I wish to send which are generated by code I’m not posting, but they are all stored in char arrays. Formulating an appropriate URL (as a char array, not String) is incredibly vexing for me. I used to do this sort of thing in Python so modifying strings was easy as pie. Now I’m trying to heed the warnings and stay away from Strings in C++ but I’m finding string (char) manipulation really confusing. Would I be better off just using String(URL) and adding/appending to this to build the URL?

One approach I’ve tried is just using strncat to pull together a URL from multiple char arrays. The code is messy and surely not the right approach?? Could someone please break it down for me in simple terms if this a bad approach and point me to some code or a tutorial which will help me understand the best way forward?

Dealing with chars and understanding how to manage memory etc in Arduino is proving to be the biggest hurdle to me actually pulling together all the parts of my project.

Here is a cut down version of what I’m trying to do with the datapoints already defined.

It does the job but surely there’s a more elegant or easier/better way?

Thanks ! :slight_smile:

const char* apiKeyValue = "kjkljad983p112";
const char* brand = "UA1";
const char* regType = "IN"; // is this node Before (IN) or after (OUT) processing

// Dynamically assigned by other functions.
char loadID[5] = "Blue";
char boxID[4] = "922" ; // split from char array so leaving as char
char boxNum[3] = "2" ; // split from char array so leaving as char

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  postData();
}


void postData() {
  // Want to format a http POST reguest to send data to sql server
  // ===== POST Data fields =====
  // PHP REF      defined char holding data
  //  -----       ------------------------
  // api_key   // apiKey
  // brand    // brand
  // regtype  // regType
  // loadID   // loadID
  // rfid     // nuidChar
  // boxid    // boxID
  // boxnum   // boxNum
  // weight   // weight
//  HTTPClient http;                  //Declare object of class HTTPClient
  char POST_URL[200] = "http://";   //start formatting POST_URL
  char IPChar[16] = "192.168.20.11";
  char nuidChar[16] = "123:142:145:992";
//  serverIp.toString().toCharArray(IPChar,16);
  // Add IP Adress of Server
  int IPlen = strlen(IPChar);
  strncat(POST_URL, IPChar, IPlen);
  // Add api_key
  strcat(POST_URL,"/post-extraction-data.php?api_key=");
  int keylen = strlen(apiKeyValue);
  strncat(POST_URL,apiKeyValue,keylen);
  // Add brand
  strcat(POST_URL,"&brand=");
  int brandlen = strlen(brand);
  strncat(POST_URL,brand,brandlen);
  // Add regType
  strcat(POST_URL,"&regtype=");
  int reglen = strlen(regType);
  strncat(POST_URL,regType,reglen);
  // Add loadID
  strcat(POST_URL,"&loadid=");
  int loadidlen = strlen(loadID);
  strncat(POST_URL,loadID,loadidlen);
  // Add rfid
  strcat(POST_URL,"&rfid=");
  int nuidCharlen = strlen(nuidChar);
  strncat(POST_URL,nuidChar,nuidCharlen);
  // Add boxID
  strcat(POST_URL,"&boxid=");
  int boxIDlen = strlen(boxID);
  strncat(POST_URL,boxID,boxIDlen);
  // Add boxNum
  strcat(POST_URL,"&boxnum=");
  int boxNumlen = strlen(boxNum);
  strncat(POST_URL,boxNum,boxNumlen);

  Serial.println("This is the formatted POST request thus far: ");
  Serial.println(POST_URL);  
}

why not try to make things easier to read

char POST_URL[200] = {};

void
append (
    const char *s)
{
    strncat (POST_URL, s, strlen(s));
    Serial.println (POST_URL);
}

void
append2 (
    const char *s,
    const char *t )
{
    strncat (POST_URL, s, strlen(s));
    strncat (POST_URL, t, strlen(t));
    Serial.println (POST_URL);
}

// -----------------------------------------------------------------------------
void
postData ()
{
    Serial.println ("__func__");
    // Want to format a http POST reguest to send data to sql server
    // ===== POST Data fields =====
    // PHP REF      defined char holding data
    //  -----       ------------------------
    // api_key   // apiKey
    // brand    // brand
    // regtype  // regType
    // loadID   // loadID
    // rfid     // nuidChar
    // boxid    // boxID
    // boxnum   // boxNum
    // weight   // weight
    //  HTTPClient http;
    //Declare object of class HTTPClient
    //
    char IPChar[16] = "192.168.20.11";
    char nuidChar[16] = "123:142:145:992";

    append ("http://");
    append (IPChar);

    append2 ("/post-extraction-data.php?api_key=", apiKeyValue);
    append2 ("&brand=", brand);
    append2 ("regtype=", regType);
    append2 ("&loadid=", loadID);

    append2 ("rfid=", nuidChar);
    append2 ("&boxid=", boxID);
    append2 ("&boxnum=", boxNum);

    append (nuidChar);

    Serial.println("This is the formatted POST request thus far: ");
    Serial.println(POST_URL);
}

Didn't think of writing my own function to make my life easy :confused:

Really need to change the way I think about these things!! I'm so used to just relying on inbuilt methods.

Thanks for your advice!!