POST Request from BoronLTE String/Int Concatenation

Heya,

Trying to post to an API that requires values to be Integers, can’t seem to concatenate strings and integers.

POST Request API:
:request body: val1 (int), val2 (int), val3 (int), val4 (int), val5 (optional int)

POST Request Application Firmware (C++)
//convert sensor int data to string
String valone(valoneint);
String valttwo(valtwoint);
String valthreeString(valthreeint);
String valfour(valfourint);
String valfiveString(valfiveint)

request.body = “{val1:”+valoneString+",val2:"+valtwoString+",val3:"+valthreeString+",valfour:"+valfourString+",valfive:"+valfiveString+"}";

The above code is the only way I can get it to compile but I can’t make a successful post to the API using this code since it expects integers, any help is appreciated.

A standing advice is to avoid String wherever possible and with that said, I'd suggest using snprintf() for your string building

  char body[64]; 
  snprintf(body, sizeof(body)
          , "{val1:%d, val2:%d, val3:%d, val4:%d, val5:%d}"
          , valoneint
          , valtwoint
          , valthreeint
          , valfourint
          , valfilveint
          );
  request.body = body;

But if you insist in using String you'd do it this way

  String someString = "literal" + String(intval);

Not sure I understand that sentence correctly.
Do you mean that API expects these integers in their binary form?
If so you cannot use request.body as this is a string and as such would not be able to hold any integer values where at least one of the four bytes in the int would be 0x00 as it would terminate the string there and then.
And this would also be a rather obscure API for my feeling :wink:

However, if your enpoint expects a JSON string, shouldn't the keys be wrapped in double quotes?

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.