I have been successfully getting the response from my web server. The problem is I get the whole package with header and content/body:

Recv: 325 bytes: HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
X-Powered-By-Plesk: PleskWin
Date: Wed, 02 Aug 2017 03:26:37 GMT
Connection: close
Content-Length: 18


I want to only assign this 1501644397698.2585 to my double variable. How can I do this?

This is the code which sends and prints the response:

Serial.println("waiting to fetch...");
sim808.send(http_cmd, sizeof(http_cmd)-1);
while (true) {
  int ret = sim808.recv(buffer, sizeof(buffer)-1);
  if (ret <= 0) {
    Serial.println("fetch over...");
  buffer[ret] = '\0';
  Serial.print("Recv: ");
  Serial.print(" bytes: ");

You need to receive the whole header and them trim what you don't want. The easiest way of doing this is scanning the header string for a new line (\n) character. From the start of the string to the new line gives you a line of the header. If the length of this substring is < 1 then look for the next line. Now scan between the start and the new line and see if you see a colon (:), if you do then this line is part of the header, look for the next new line. Otherwise use strtod() to convert the data to a double.

It should be something like this (not compiled, not tested):

char* response = ".........";
char* pStartOfLine = response;
char* pEndOfLine = response;
bool bFoundData = false;
while (!bFoundData && pEndOfLine)
  while (pEndOfLine != NULL && *pEndOfLine != '\n')
  if (pEndOfLine && pEndOFLine != pStartOfLine)
    char* pStartOfData = pStartOfLine;
    while (pStartOfLine != pEndOfLine && *pStartOfLine != ':')
    if (*pStartOfLine != ':')
      theData = strtod (pStartOfData, NULL);
      bFoundData = true;

If it's in a separate function then you could just return rather than use the Boolean.

The order of the lines in the header are not guaranteed, but will always be in the same order from the same device with the same software, so as a cheat you could look for 'Content-Length: 18\r\n' and then just take the next non-blank line after that, but this is a more hacky way of doing it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.