Reading data returned from BlynkAPI call Issue


I am using Blynk Local Server Version 0.39.2

I’ve noticed for a while that when I make a Blynk API call and process the returned messages that I always hit my client TIMEOUT. I’ve realized that the text/string returned from Blynk Server does not have a ‘\n’ end of line marker.

I just wanted to confirm is this by design ? (perhaps I’m doing something wrong)

I’m using the following code to read the result and it obvious reading it by I always hit the TIMEOUT as indicate in commented code extract below.

				while (client.connected())
					line = client.readStringUntil('\n');  // *** this works fine .. reads in the http header info
					if (line.startsWith("content-length"))
						Serial.println("found content-length");

					if (line == "\r")

						Serial.println("--- All headers received --- "); 
				} // end of while loop

				line = client.readStringUntil('\n');  // *** this is where I read the Blynk Server content and find the '\n" is never found and thus I hit the 4000 ms client timeout.

‘\n’ is not necessary the end of the request. If request has body, than you need to read a number of bytes specified in content-length header.

thanks for reply Dmitriy.
now I’m using content-length to determine the number of bytes to read and actually reading bytes using following code (pasted below if in case others want to do the same):

char body_content[content_length_int+1];  // + 1 to add the '\0' which is not critical but makes screen prints work better
client.readBytes(body_content, content_length_int);
body_content[content_length_int] = '\0';  // this the last added character space as first space is cell [0]
Serial.print ("body_content = "); Serial.println (body_content);

it works well.

Although would be nice if the body message was terminated with a ‘\n’ as that would mean no need to extract content-length value from header :wink: but no big deal.

Content-length is a must for requests with a body according to HTTP specification. So you have to read it.

1 Like