11#include " Client.h"
22#include " RestClient.h"
33
4+ // #define HTTP_DEBUG true
45#ifdef HTTP_DEBUG
56#define HTTP_DEBUG_PRINT (string ) (Serial.print(string))
67#endif
@@ -16,6 +17,8 @@ RestClient::RestClient(Client& netClient, const char* _host) {
1617 num_headers = 0 ;
1718 contentType = " x-www-form-urlencoded" ; // default
1819 this ->client = &netClient;
20+ this ->responseBody = " " ;
21+ this ->timeout = 1000 ; // default. TODO: add a setter function
1922}
2023
2124RestClient::RestClient (Client& netClient, const char * _host, int _port) {
@@ -26,159 +29,119 @@ RestClient::RestClient(Client& netClient, const char* _host, int _port) {
2629 this ->client = &netClient;
2730}
2831
29-
30-
31-
3232// GET path
3333int RestClient::get (const char * path){
34- return request (" GET" , path, NULL , NULL );
35- }
36-
37- // GET path with response
38- int RestClient::get (const char * path, String* response){
39- return request (" GET" , path, NULL , response);
34+ return request (" GET" , path, " " );
4035}
4136
4237// POST path and body
43- int RestClient::post (const char * path, const char * body){
44- return request (" POST" , path, body, NULL );
45- }
46-
47- // POST path and body with response
48- int RestClient::post (const char * path, const char * body, String* response){
49- return request (" POST" , path, body, response);
38+ int RestClient::post (const char * path, String body){
39+ return request (" POST" , path, body);
5040}
5141
5242// PUT path and body
53- int RestClient::put (const char * path, const char * body){
54- return request (" PUT" , path, body, NULL );
55- }
56-
57- // PUT path and body with response
58- int RestClient::put (const char * path, const char * body, String* response){
59- return request (" PUT" , path, body, response);
43+ int RestClient::put (const char * path, String body){
44+ return request (" PUT" , path, body);
6045}
6146
6247// DELETE path
6348int RestClient::del (const char * path){
64- return request (" DELETE" , path, NULL , NULL );
65- }
66-
67- // DELETE path and response
68- int RestClient::del (const char * path, String* response){
69- return request (" DELETE" , path, NULL , response);
49+ return request (" DELETE" , path, " " );
7050}
7151
7252// DELETE path and body
73- int RestClient::del (const char * path, const char * body ){
74- return request (" DELETE" , path, body, NULL );
75- }
76-
77- // DELETE path and body with response
78- int RestClient::del (const char * path, const char * body, String* response){
79- return request (" DELETE" , path, body, response);
53+ int RestClient::del (const char * path, String body ){
54+ return request (" DELETE" , path, body);
8055}
8156
82- void RestClient::write (const char * string){
83- HTTP_DEBUG_PRINT (string);
84- client->print (string);
85- }
86-
87- void RestClient::setHeader (const char * header){
57+ void RestClient::setHeader (String header){
8858 headers[num_headers] = header;
8959 num_headers++;
9060}
9161
92- void RestClient::setContentType (const char * contentTypeValue){
62+ void RestClient::setContentType (String contentTypeValue){
9363 contentType = contentTypeValue;
9464}
9565
9666// The mother- generic request method.
9767//
98- int RestClient::request (const char * method, const char * path,
99- const char * body, String* response){
68+ int RestClient::request (const char * method, String path, String body){
10069
10170 HTTP_DEBUG_PRINT (" HTTP: connect\n " );
10271
10372 if (client->connect (host, port)){
10473 HTTP_DEBUG_PRINT (" HTTP: connected\n " );
10574 HTTP_DEBUG_PRINT (" REQUEST: \n " );
10675 // Make a HTTP request line:
107- write (method);
108- write (" " );
109- write (path);
110- write (" HTTP/1.1\r\n " );
76+ client-> print (method);
77+ client-> print (" " );
78+ client-> print (path);
79+ client-> println (" HTTP/1.1" );
11180 for (int i=0 ; i<num_headers; i++){
112- write (headers[i]);
113- write (" \r\n " );
81+ client->println (headers[i]);
11482 }
115- write (" Host: " );
116- write (host);
117- write (" \r\n " );
118- write (" Connection: close\r\n " );
119-
120- if (body != NULL ){
121- char contentLength[30 ];
122- sprintf (contentLength, " Content-Length: %d\r\n " , strlen (body));
123- write (contentLength);
124-
125- write (" Content-Type: " );
126- write (contentType);
127- write (" \r\n " );
83+ client->print (" Host: " );
84+ client->println (host);
85+ client->println (" Connection: close" );
86+
87+ // TODO: POST is not sending body right now. Not sure why not
88+ if (body != " " ){
89+ // char contentLength[30];
90+ // sprintf(contentLength, "Content-Length: %d", body.length());
91+ client->print (" Content-Length: " );
92+ client->println (body.length ());
93+
94+ client->print (" Content-Type: " );
95+ client->println (contentType);
96+ HTTP_DEBUG_PRINT (body.length ());
97+ HTTP_DEBUG_PRINT (contentType);
98+ client->println (); // empty line after headers
99+ client->println (body);
100+ HTTP_DEBUG_PRINT (body);
128101 }
129-
130- write (" \r\n " );
131-
132- if (body != NULL ){
133- write (body);
134- write (" \r\n " );
135- write (" \r\n " );
136- }
137-
138- // make sure you write all those bytes.
102+ client->println ();
103+ // make sure you've sent all bytes. Ugly hack.
104+ // TODO: check output buffer instead
139105 delay (100 );
140106
141- HTTP_DEBUG_PRINT (" HTTP: call readResponse \n " );
142- int statusCode = readResponse (response );
143- HTTP_DEBUG_PRINT (" HTTP: return readResponse \n " );
107+ HTTP_DEBUG_PRINT (" HTTP: call getResponse \n " );
108+ int statusCode = getResponse ( );
109+ HTTP_DEBUG_PRINT (" HTTP: return getResponse \n " );
144110
145111 // cleanup
146- HTTP_DEBUG_PRINT (" HTTP: stop client\n " );
147- num_headers = 0 ;
148- client->stop ();
149- delay (50 );
150- HTTP_DEBUG_PRINT (" HTTP: client stopped\n " );
151-
112+ // only stop if server disconnected. Otherwise you can
113+ // get in an infinite loop in getResponse:
114+ if (!client->connected ()) {
115+ HTTP_DEBUG_PRINT (" HTTP: stop client\n " );
116+ num_headers = 0 ;
117+ client->stop ();
118+ HTTP_DEBUG_PRINT (" HTTP: client stopped\n " );
119+ }
152120 return statusCode;
153121 }else {
154122 HTTP_DEBUG_PRINT (" HTTP Connection failed\n " );
155123 return 0 ;
156124 }
157125}
158126
159- int RestClient::readResponse (String* response ) {
160-
127+ int RestClient::getResponse ( ) {
128+ this -> requestStart = millis ();
161129 // an http request ends with a blank line
162130 boolean currentLineIsBlank = true ;
163131 boolean httpBody = false ;
164132 boolean inStatus = false ;
133+ // clear response string:
134+ this ->responseBody = " " ;
165135
166136 char statusCode[4 ];
167137 int i = 0 ;
168138 int code = 0 ;
169139
170- if (response == NULL ){
171- HTTP_DEBUG_PRINT (" HTTP: NULL RESPONSE POINTER: \n " );
172- }else {
173- HTTP_DEBUG_PRINT (" HTTP: NON-NULL RESPONSE POINTER: \n " );
174- }
175-
176140 HTTP_DEBUG_PRINT (" HTTP: RESPONSE: \n " );
177141 while (client->connected ()) {
178- HTTP_DEBUG_PRINT (" ." );
179-
142+ // HTTP_DEBUG_PRINT(".");
180143 if (client->available ()) {
181- HTTP_DEBUG_PRINT (" ," );
144+ // HTTP_DEBUG_PRINT(",");
182145
183146 char c = client->read ();
184147 HTTP_DEBUG_PRINT (c);
@@ -197,8 +160,8 @@ int RestClient::readResponse(String* response) {
197160 }
198161
199162 if (httpBody){
200- // only write response if its not null
201- if (response != NULL ) response-> concat (c) ;
163+ // add this char tot the responseBody
164+ this -> responseBody += c ;
202165 }
203166 else
204167 {
@@ -215,9 +178,13 @@ int RestClient::readResponse(String* response) {
215178 currentLineIsBlank = false ;
216179 }
217180 }
181+ } else {
182+ // there is a condition where client connects
183+ // and available() always <= 0. So timeout is here to catch that:
184+ if (millis () - this ->requestStart > this ->timeout ) return 0 ;
218185 }
219186 }
220187
221- HTTP_DEBUG_PRINT (" HTTP: return readResponse3 \n " );
188+ HTTP_DEBUG_PRINT (" HTTP: return getResponse3 \n " );
222189 return code;
223190}
0 commit comments