简体   繁体   中英

How I can get the body from a HTTP response?

Hey I am doing and Arduino project, and I want to get the body from an HTTP request, here is the code:

// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
             "Host: " + host + "\r\n" +
             "Connection: close\r\n\r\n");
delay(100);
int liniea_info = 0;
while(client.available()){
    String line = client.readStringUntil('\r');
    if(liniea_info == 13){Serial.print(line);}
    Serial.print(liniea_info);
    ++liniea_info;
}

This works good, without the integer liniea_info returns me this:

Requesting URL: /output/***.csv?colors=11
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: X-Requested-With
Content-Type: text/plain
Transfer-Encoding: chunked
Date: Sat, 17 Jun 2017 08:09:31 GMT
Connection: close
Set-Cookie: SERVERID=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/
Cache-control: private

34
colors,day,timestamp
11,12,2017-06-17T07:48:10.619Z

0

I thought that with the int liniea_info I will get only the line that I want that is the line "11,12,2017-06-17T07:48:10.619Z" , but no, with this only prints the first line.

Anyone sees what I am doing wrong or how to do it?

Don't reinvent the wheel. Just use HTTPClient library. This library handles request and response.

Add to includes:

#include <ESP8266HTTPClient.h>

And simply:

HTTPClient http;
http.begin("http://www.sample-videos.com/csv/Sample-Spreadsheet-10-rows.csv");
int statusCode = http.GET();
Serial.println(http.getString());
http.end();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM