Go Down

Topic: Send POST Request with custom JSON - MKR1000 (Read 1 time) previous topic - next topic

jtonsich

Hi! I have to send a POST request with this JSON:

 json.prettyPrintTo(Serial);
 /*
 {
  "username": "user",
  "password": "pass,
  "inputs": [
    {
      "name": "round",
      "value": 6
    },
    {
      "name": "light",
      "value": 15
    },
    {
      "name": "temperature",
      "value": 70.60547
    }
  ]
 }
 */
 
 json.printTo(Serial);
 // {"username":"user","password":"pass","inputs":[{"name":"round","value":1},{"name":"light","value":16},{"name":"temperature","value":73.53516}]}
 

Using POSTMAN I must add the following fields:

In Headers section, Content-Type = application/json
In Body section, I select raw, and JSON (application/json)

This works fine. But in Arduino, i dont know if my code is correct, it doesn't work:
(I could send an email, so the problem isn't with the wifi conection)

//************************************************************************
//                               POST REQUEST
//************************************************************************

void post(float temp, int light, int count) {
 WiFiClient client;
 String httpsRequest = "https://mydomain.com";
 const char* host = "mydomain.com";
 
 // CONVERT VALUES TO STRING
 String tempS = String(temp);
 String lightS = String(light);
 String countS = String(count);
 String jsonS = "{(\"username\":\"user\",\"password\":\"pass\"}";

 if (client.connect(host, 922)) {
   Serial.println("Connected to client");
   client.println("POST http://mydomain.com:port/blablabla HTTP/2.0");
   client.println("Host: mydomain.com");
   client.println("Connection: close");
   client.println("Content-Type: application/json");
   client.println(jsonS);
 
   delay(2000);
   
   while (client.available()) { // Print on the console the answer of the server
     char c = client.read();
     Serial.write(c);
   }
   client.stop();  // Disconnect from the server
 }
 else {
   Serial.println("Failed to connect to client");
 }
}

Any ideas?

Go Up