Go Down

Topic: Skip HTTP Header from GET request (Read 89 times) previous topic - next topic

luizgunner

I need help with my project.
I use Arduino Mega + ESP8266 for communication with Node.js in my local server.

Here is my code:

Code: [Select]
void ConexaoServer(){
    uint8_t buffer[1024] = {0};

    if (wifi.createTCP(HOST_NAME, HOST_PORT)) {
        Serial.print("Conexao com o Host OK!\r\n");
    } else {
        Serial.print("Conexao com o Host com ERRO!\r\n");
    }

    char *ComandoGET = "GET /teste HTTP/1.1\r\nHost: SmartHome\r\nConnection: close\r\n\r\n";
    wifi.send((const uint8_t*)ComandoGET, strlen(ComandoGET));

    uint32_t len = wifi.recv(buffer, sizeof(buffer), 10000);
    if (len > 0) {
        Serial.print("Received:[");
        for(uint32_t i = 0; i < len; i++) {
            Serial.print((char)buffer[i]);
        }
        Serial.print("]\r\n\r\n");
    }
}


And i receive from Node.js in Serial

Code: [Select]
Received:[HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: text/html; charset=utf-8
Content-Length: 4
ETag: W/"4-wArb1VtkpsN6In8g50pGNw"
Date: Wed, 31 Aug 2016 01:26:41 GMT
Connection: close

Luiz]


But i want to receive just "Received:[Luiz]". How i can do this?

Thanx!

PaulS

Quote
But i want to receive just "Received:[Luiz]". How i can do this?
You can't. You have NO control over what the server sends.

You could, of course, read and discard all the data, until the blank line arrives. When that happens, you could then store the data.


zhomeslice

These are some functions I use to help me search for stuff int strings. you could use these to find the start and end of what you want and dump the rest

Code: [Select]
int strpos(String haystack, String needle,int offset)
{
  int foundpos = -1;
  for (offset; (offset <= haystack.length() - needle.length()); offset++) {
    if (haystack.substring(offset,needle.length()+offset) == needle) {
      foundpos = offset;
    }
  }
  return foundpos;
}
int strpos(char * haystack, char * needle,int offset)
{
  char * Pointer;
  Pointer = strstr(haystack+offset, needle)+offset;
  return(&Pointer[0] - &haystack[0]);
}


String str_replace(String Search, String Replace, String Subject){
  Subject.replace(Search, Replace);
  return(Subject);
}
HC

luizgunner

You can't. You have NO control over what the server sends.

You could, of course, read and discard all the data, until the blank line arrives. When that happens, you could then store the data.


Hmmm, i undertand, but how i can do this?

These are some functions I use to help me search for stuff int strings. you could use these to find the start and end of what you want and dump the rest

Code: [Select]
int strpos(String haystack, String needle,int offset)
{
  int foundpos = -1;
  for (offset; (offset <= haystack.length() - needle.length()); offset++) {
    if (haystack.substring(offset,needle.length()+offset) == needle) {
      foundpos = offset;
    }
  }
  return foundpos;
}
int strpos(char * haystack, char * needle,int offset)
{
  char * Pointer;
  Pointer = strstr(haystack+offset, needle)+offset;
  return(&Pointer[0] - &haystack[0]);
}


String str_replace(String Search, String Replace, String Subject){
  Subject.replace(Search, Replace);
  return(Subject);
}


U can comment the code? Thanks!

zhomeslice

Here is some examples as to how the functions work while searching for the start and end of what you are looking for in the returned strings

Code: [Select]


char *Haystack = "This is a test schetch and it dimonstrates the strpos() and str_replace() funcitons.";
char *Needle = "and";
String HaystackString = "I perfer using char * for strings but you can do it this way also";
String NeedleString = "you can do it";
int pos;
String NewString;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);                   // diagnostic channel
  Serial.println(Haystack);
  // using char *
  pos = strpos(Haystack,Needle,0); // Haystack is what you are looking in, Needle is what you are looking for, offset is to start looking at a point further into the string
  Serial.print("We found the first and at:");  Serial.println(pos);
  pos = strpos(Haystack,Needle,pos + 1); // Lets start looking for the second instance of and by offsetting the start by one character from the first instance of and
  Serial.print("We found the Second and at:");  Serial.println(pos);
  // using String
  pos = strpos(HaystackString,NeedleString,0); // Lets start looking for the second instance of and by offsetting the start by one character from the first instance of and
  Serial.print("We found the 'you can do it' at:");  Serial.println(pos);
  NewString = str_replace("this way also", "like this", HaystackString);
  Serial.print("OldString:");  Serial.println(HaystackString);
  Serial.print("NewString:");  Serial.println(NewString);
 
 
}

void loop() {
  // put your main code here, to run repeatedly:


}

int strpos(String haystack, String needle, int offset)
{
  int foundpos = -1;
  for (offset; (offset <= haystack.length() - needle.length()); offset++) {
    if (haystack.substring(offset, needle.length() + offset) == needle) {
      foundpos = offset;
    }
  }
  return foundpos;
}
int strpos(char * haystack, char * needle, int offset)
{
  char * Pointer;
  Pointer = strstr(haystack + offset, needle) + offset;
  return (&Pointer[0] - &haystack[0]);
}


String str_replace(String Search, String Replace, String Subject) {
  Subject.replace(Search, Replace);
  return (Subject);
}
HC

lastchancename

Once you get the String.x example running, and understand the logic... rewrite it without the String class (using C char handling).

Less memory, faster and far more flexible.
Q: How many searches did you make before posting this question?      A: none
At the very least, take a guess at the solution, then we can help move forward from what you know already.

PaulS

Quote
Once you get the String.x example running, and understand the logic... rewrite it without the String class (using C char handling).

Less memory, faster and far more flexible.
Since you only need to store at most 3 characters to determine if it is time to store the important data, that example, while interesting, is not necessary.

If you get a \n, save it. If not, discard it. If the next character is a \r (as it should be), you have received an end of record marker. If the next character is not a \n, the line is not blank, so you don't care about that data.

A brute force method to do this is:
Save each character read into the last element of a 4 element array, after shifting the existing last three characters up a position.

Compare the array to "\n\r\n\r". If they match, then the remaining data coming from the server is the data you want. If not, keep reading and checking.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy