I have a Adafruit huzzaah ESP32 and I am trying to make a request to a web service to get json information. The call completes successfully returning Status Code 200 but when I call getString() it just stops (I dont think crashes) just nothing appears in the Serial window
The issue is that you appear to have made no effort to debug the library. Why not?
You can add Serial.print() statements to the library. You can make the same request using a browser, and tell us whether the json data is 27 characters or 27 megabytes of data.
PaulS:
The issue is that you appear to have made no effort to debug the library. Why not?
You can add Serial.print() statements to the library. You can make the same request using a browser, and tell us whether the json data is 27 characters or 27 megabytes of data.
The data returned from the call is a Json Web token in a Json object with one field and that is it. I already said this request executes successfully (tried via postman and just in browser)
In the HttpClient library its stuck in this while loop
/**
* write one Data Block to Stream
* @param stream Stream *
* @param size int
* @return < 0 = error >= 0 = size written
*/
int HTTPClient::writeToStreamDataBlock(Stream * stream, int size)
{
int buff_size = HTTP_TCP_BUFFER_SIZE;
int len = size;
int bytesWritten = 0;
// if possible create smaller buffer then HTTP_TCP_BUFFER_SIZE
if((len > 0) && (len < HTTP_TCP_BUFFER_SIZE)) {
buff_size = len;
}
// create buffer for read
uint8_t * buff = (uint8_t *) malloc(buff_size);
if(buff) {
// read all data from server
while(connected() && (len > 0 || len == -1)) { //Stuck looping here
Serial.println("while");
// get available data size
size_t sizeAvailable = _tcp->available();
if(sizeAvailable) {
int readBytes = sizeAvailable;
// read only the asked bytes
if(len > 0 && readBytes > len) {
readBytes = len;
}
// not read more the buffer can handle
if(readBytes > buff_size) {
readBytes = buff_size;
}
// read data
int bytesRead = _tcp->readBytes(buff, readBytes);
// write it to Stream
int bytesWrite = stream->write(buff, bytesRead);
bytesWritten += bytesWrite;
// are all Bytes a writen to stream ?
if(bytesWrite != bytesRead) {
Serial.println("short write asked for %d but got %d retry...")
log_d("short write asked for %d but got %d retry...", bytesRead, bytesWrite);
// check for write error
if(stream->getWriteError()) {
Serial.println("stream write error %d")
log_d("stream write error %d", stream->getWriteError());
//reset write error for retry
stream->clearWriteError();
}
// some time for the stream
delay(1);
int leftBytes = (readBytes - bytesWrite);
// retry to send the missed bytes
bytesWrite = stream->write((buff + bytesWrite), leftBytes);
bytesWritten += bytesWrite;
if(bytesWrite != leftBytes) {
// failed again
Serial.println("short write asked for %d but got %d failed.")
log_w("short write asked for %d but got %d failed.", leftBytes, bytesWrite);
free(buff);
return HTTPC_ERROR_STREAM_WRITE;
}
}
// check for write error
if(stream->getWriteError()) {
Serial.println("stream write error %d")
log_w("stream write error %d", stream->getWriteError());
free(buff);
return HTTPC_ERROR_STREAM_WRITE;
}
// count bytes to read left
if(len > 0) {
len -= readBytes;
}
delay(0);
} else {
delay(1);
}
}
free(buff);
Serial.println("connection closed or file end (written: %d).")
log_d("connection closed or file end (written: %d).", bytesWritten);
if((size > 0) && (size != bytesWritten)) {
Serial.println("bytesWritten %d and size %d mismatch!.")
log_d("bytesWritten %d and size %d mismatch!.", bytesWritten, size);
return HTTPC_ERROR_STREAM_WRITE;
}
} else {
Serial.println("too less ram! need %d")
log_w("too less ram! need %d", HTTP_TCP_BUFFER_SIZE);
return HTTPC_ERROR_TOO_LESS_RAM;
}
return bytesWritten;
}
Thanks I am well aware of what a function is and a while loop, you complained I didnt do any debugging in the library (which I didnt know I could do) so I posted exactly the problem area with a comment in the function on the while loop that it is stuck on (Note the "Stuck looping here" in the code).
If all you dont want to help and just give me condescending answers then fine you can move along, my fault for being new to arduino architecture and what all you can/cant do while trying to do some sample things to learn more
tyczj:
Thanks I am well aware of what a function is and a while loop, you complained I didnt do any debugging in the library (which I didnt know I could do) so I posted exactly the problem area with a comment in the function on the while loop that it is stuck on (Note the "Stuck looping here" in the code).
If all you dont want to help and just give me condescending answers then fine you can move along, my fault for being new to arduino architecture and what all you can/cant do while trying to do some sample things to learn more
What I expected to see was some serial output that I could compare to the code. I'm sorry that my expectations didn't meet with your approval.
What IS the relationship between that function and the getString() function of whatever class http is a instance of?
String HTTPClient::getString(void)
{
Serial.println("Getting string");
StreamString sstring;
if(_size) {
// try to reserve needed memmory
if(!sstring.reserve((_size + 1))) {
Serial.println("not enough memory to reserve a string");
log_d("not enough memory to reserve a string! need: %d", (_size + 1));
return "";
}
}
writeToStream(&sstring);
return sstring;
}
calls a method writeToStream which on an if statement that I am seeing being hit
if(_transferEncoding == HTTPC_TE_IDENTITY) {
Serial.println("HTTPC_TE_IDENTITY");
ret = writeToStreamDataBlock(stream, len);
Serial.println(ret);
// have we an error?
if(ret < 0) {
Serial.println("Error");
return returnError(ret);
}
}
calls the writeToStreamDataBlock method
The output show it finds an initial 462 bytes then finds nothing else (which is the correct number for this specific request) so its looking for something that it will never find
Console:
HTTPC_TE_IDENTITY
while
462
Available
462
Bytes read: 462
Bytes write: 462
Bytes written462
Len: -1
while
0
Not available
while
0
Not available
etc....
If I just break out of the loop when it gets to the end I get all the data fine and can continue on with everything else but I dont know what that will break
The output that you show doesn't match what I would expect to see from the code you posted. There is more of it.
The numbers that you are printing are useless without knowing what variable you are printing the contents of.
It would appear, though, that somehow, somewhere, the ESP decided that 462 - 462 = -1.
What I would do is, in the while loop, after decrementing the number of bytes still to be written, test for the result being 0. If that happens, and it should when all the data is written, break; out of the while loop.