[SOLVED] HttpClient POST with Multiple Values

I have successfully tested HttpClient with GET and POST using a single value. However is it possible to send multiple values via one POST request? I believe my server is expecting JSON formatted data.
I don’t know if there are any limitations on what can be sent via HttpClient. If there are limitations I will pursue webhooks instead.

request.body =  "{\"variable1\": " + String(value1) + "}";
// works

request.body =  "{\"variable1\": " + String(value1) + "}, " +
                "{\"variable2\": " + String(value2) + "}";
// does not work

request.body =  "{ {\"variable1\": " + String(value1) + "}, " +
                "{\"variable2\": " + String(value2) + "} }";
// does not work

request.body =  "{\"variable1\": " + String(value1) + "} " +
                "{\"variable2\": " + String(value2) + "}";
// does not work

Any help would be appreciated. Thanks.

Try sprintf() or String::format() (which uses the same format as sprintf())

Also once you terminate a C code line with ; like here

request.body =  "{ {\"variable1\": " + String(value1) + "}, ";
                "{\"variable2\": " + String(value2) + "} }";

The following line won’t build.

This should at least build.

request.body =  "{ {\"variable1\": " + String(value1) + "}, " +
                "{\"variable2\": " + String(value2) + "} }";
// or if these are int values
request.body =  String::format("{ {\"variable1\": %d }, {\"variable2\": %d } }", value1, value2);

Sorry I was just rewriting what I tried before and I wrote it wrong previously. The strings do build, and I write them out to verify the array format is what I intended.

Your suggested code:

request.body =  String::format("{ {\"variable1\": %d }, {\"variable2\": %d } }", value1, value2);

provides the same strings that I was trying to generate, although it may be a more proper way to create them. I’ll try to troubleshoot some more.

It looks like I was using the incorrect format for the values being sent to my Laravel server. This works to send multiple values at once. Hopefully it can help someone else in the future.

request.body =  "{\"variable1\": " + String(value1) +
                ", \"variable2\": " + String(value2) + "}";

Or this way:

request.body =  String::format("{\"variable1\": %d, \"variable2\": %d }", value1, value2);