title: Arduino POST request using JSON
body: <p>I am trying to get my Arduino (with an Ethernet shield) to send a POST request with a JSON body to my locally hosted server. </p>
  
  <p>I am using the <strong>ArduinoJson</strong> (version 6) and <strong>Ethernet</strong> libraries.</p>
  
  <p>I am attempting to send a <strong>POST</strong> request to a local endpoint (hosted on my laptop) <strong>/routes/test</strong> by using ArduinoJSON's JSON creation functionality. Using the library, I create a DynamicJsonDocument named <em>doc</em> and write attributes to it. I then use <em>serializeJson</em> to write the doc's data to the POST request. </p>
  
  <p><strong>My Problem:</strong> I make a POST request to an endpoint <strong>/routes/test</strong> but when I console.log the body, it appears as empty. It seems like I am not including <em>any</em> body in my post request</p>
  
  <p>Using <code>utils.inspect</code> on the request object, I get this:</p>
  
  <blockquote>
    <p>body: {}</p>
  </blockquote>
  
  <p><strong>Is this a syntax issue or is there something fundamentally wrong with my method? Any help would be greatly appreciated!!.</strong> </p>
  
  <pre><code>#include &lt;ArduinoJson.h&gt;
  #include &lt;Ethernet.h&gt;
  #include &lt;SPI.h&gt;
  
  byte mac[]      = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };   
  byte ip[]       = { 10, 0, 0, 177 };                        
  byte server[]   = { x, x, x, x };  
  
  EthernetClient client;
  
  void setup() {
  
    //Initialize Ethernet and account for 1s delay
    Ethernet.begin(mac, ip);
    Serial.begin(9600);
    delay(1000);
  
    Serial.println("connecting...");
  
    if (client.connect(server, 5000)) {
      Serial.println("connected");
    } else {
      Serial.println("connection failed");
    }
  
    //Create JSON doc and write a "name" attribute
    const size_t capacity = JSON_OBJECT_SIZE(1);
    DynamicJsonDocument doc(capacity);
    doc["name"] = "some random name";
  
  
    //POST request
    Serial.println("Begin POST Request");
  
    client.println("POST /routes/test");
    client.println();
    client.println("Host:  x.x.x.x");
    client.println("User-Agent: Arduino/1.0");
    client.println("Content-Type: application/json;charset=UTF-8");
    client.println("Connection: close");
    client.print("Content-Length: ");
    client.println(measureJson(doc));
  
    //This works like client.println, but prints doc to client
    serializeJson(doc, client);
  
    //To let me know that request has been completed
    Serial.println("Sent Get Request");
  
  
  </code></pre>