# Parse HTTP Response

**URL:** <https://forum.arduino.cc/t/parse-http-response/523424>\
**Category:** Programming\
**Created:** [April 30, 2018, 6:22pm UTC](https://forum.arduino.cc/t/parse-http-response/523424 "2018-04-30T18:22:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![XENOTpro](https://avatars.discourse-cdn.com/v4/letter/x/5fc32e/32.png) [@XENOTpro](https://forum.arduino.cc/u/XENOTpro)\
**Post date:** [April 30, 2018, 6:22pm UTC](https://forum.arduino.cc/t/parse-http-response/523424/1 "2018-04-30T18:22:49Z")

</div>

I know that there are already other threads discussing this problem, but I couldn't find a solution there.

This is my http response:

```cpp
HTTP/1.1 200 OK
Date: Mon, 30 Apr 2018 18:17:05 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: close
Set-Cookie: __cfduid=d5ab3e4f669fb7fa723acf07372e101d21525112225; expires=Tue, 30-Apr-19 18:17:05 GMT; path=/; domain=.unkn0wn.website; HttpOnly
X-Powered-By: PHP/7.2.4
X-Powered-By: PleskLin
Server: cloudflare
CF-RAY: 413c00d3a700b6c7-LUX

a
1525112225
0

```

All I want is the actual data part. In this example only "1525112225".

I'm using the code mentioned on the arduino website:

```cpp
/*
  Web client

 This sketch connects to a website (http://www.google.com)
 using an Arduino Wiznet Ethernet shield.

 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13

 created 18 Dec 2009
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe, based on work by Adrian McEwen

 */

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
char server[] = "www.google.com";    // name address for Google (using DNS)

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /search?q=arduino HTTP/1.1");
    client.println("Host: www.google.com");
    client.println("Connection: close");
    client.println();
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop() {
  // if there are incoming bytes available
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while (true);
  }
}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 30, 2018, 6:28pm UTC](https://forum.arduino.cc/t/parse-http-response/523424/2 "2018-04-30T18:28:55Z")

</div>

> All I want is the actual data part. In this example only "1525112225".

How is the Arduino supposed to know what "the actual data part" is? As far as it is concerned, no part of the server output is more important or useful than any other part.

Personally, I find it hard to believe that you got that response from making a GET request to google.

---

<div class="post-metadata">

**Author:** ![XENOTpro](https://avatars.discourse-cdn.com/v4/letter/x/5fc32e/32.png) [@XENOTpro](https://forum.arduino.cc/u/XENOTpro)\
**Post date:** [April 30, 2018, 6:41pm UTC](https://forum.arduino.cc/t/parse-http-response/523424/3 "2018-04-30T18:41:51Z")

</div>

I just used the arduino code. Of course I changed the servers hostname to mine.

My Webserver is returning the current time, for example "1525112225".

I just want to get the actual data, in my case "1525112225" from the http reponse.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 30, 2018, 10:29pm UTC](https://forum.arduino.cc/t/parse-http-response/523424/4 "2018-04-30T22:29:16Z")

</div>

Once again, I'll remind you that "the actual data" is the ENTIRE server response.

If only SOME of the data is important, YOU must define what marks the start of important data and what marks the end of important data.

Then, when the start marker arrives, you know to start storing data in a char array, and when the end marker arrives you know to stop storing the data, use the stored data, and reset the array, and index, for the next server reply.

Why on earth would you post code that is NOT the code you are having problems with?

---

<div class="post-metadata">

**Author:** ![SparksnSmoke](https://avatars.discourse-cdn.com/v4/letter/s/df788c/32.png) [@SparksnSmoke](https://forum.arduino.cc/u/SparksnSmoke)\
**Post date:** [June 1, 2020, 3:41pm UTC](https://forum.arduino.cc/t/parse-http-response/523424/5 "2020-06-01T15:41:08Z")

</div>

I may be way too late, but what I assume you are trying to do is locate the body of the response and then choose which line to read. If so then yes it can most certainly be done.

```cpp
 while (client.available()) 
  {
    char c = client.read();
    Serial.print(c);
    
    if(c == '\n')
    {
      c = client.read();
      Serial.print(c);
      if(c == '\r')
      {
        c = client.read();
        Serial.print(c);
        if(c == '\n')
        {
          c = client.read();
          Serial.print(c);
          digitalWrite(MACHINE, c);
          Serial.println();
          Serial.print(F("Machine state: "));
          Serial.println(c);
        }
      }
    }

```

The code above is what I used to locate the response body by finding the empty line that always separates the body from the rest of the response as the blank line '\r\n' will always be preceded by a '\n'. Finding the right line in the body is not that much different either, you can differentiate each line by finding the '\n' at the end of each line. The code below should be the basic structure to parse out specific lines in the body:

```cpp
for(int i; i < body_line; i++)
{
  //Cycle through the body preceding your desired line
  while(/*Loop until end of line is reached*/)
  {
    //Print out body data until end of line is reached
  }
}

while(/*Loop until end of line is reached*/)
{
  //Use this while loop to process desired body line
}

while(/*Loop until end of response is reached*/)
{
  //Read out the remaining data
}

```

I hope this helps!

---

<div class="post-metadata">

**Author:** ![robschaap](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@robschaap](https://forum.arduino.cc/u/robschaap)\
**Post date:** [June 1, 2020, 5:10pm UTC](https://forum.arduino.cc/t/parse-http-response/523424/6 "2020-06-01T17:10:48Z")

</div>

It may not be clear, but what they're saying is that the problem your encountering is more to do with the HTTP response from the server, not the client (Arduino device). You need to wrap the response in something. In web programming the server would spit out a rest JSON formatted response like:

{ "Time": 1001010}

Not ideal to deserialize JSON, but if you just spit it out by itself, then you could say the entire reponse body is the time. Or a comma or pipe delimited string that Arduino could turn into an object and it's properties. I do REST API's for a living basically. Usually serving data to Angular, Vue or whatever framework. They all handle JSON docs, I've played with the JSON library for Arduino, but it seems like it's CPU heavy where it's most useful or overkill in a scenario like yours.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 11:09pm UTC](https://forum.arduino.cc/t/parse-http-response/523424/7 "2021-05-05T23:09:54Z")

</div>

Closed on May 5, 2021, 11:09pm UTC


