Skip to content

Update HTTPClient.cpp to send big payload #7638

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions libraries/HTTPClient/src/HTTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,9 @@ int HTTPClient::sendRequest(const char * type, uint8_t * payload, size_t size)

if(payload && size > 0) {
addHeader(F("Content-Length"), String(size));
} else {
// force 0 content length for redirect request
addHeader(F("Content-Length"), String("0"));
}

// add cookies to header, if present
Expand All @@ -619,10 +622,21 @@ int HTTPClient::sendRequest(const char * type, uint8_t * payload, size_t size)

// send Payload if needed
if(payload && size > 0) {
if(_client->write(&payload[0], size) != size) {
return returnError(HTTPC_ERROR_SEND_PAYLOAD_FAILED);

// this will fail for big payload
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of leaving the old code in comments, it would be better to simply drop it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@atanisoft Fixed. Please kindly check it. Thank you.

// if(_client->write(&payload[0], size) != size) {
// return returnError(HTTPC_ERROR_SEND_PAYLOAD_FAILED);
// }

// send the payload per 1000 bytes
log_d("Sending %u byte ...\n", size);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks like your PR is introducing a mix of TAB and SPACE characters resulting in a jumbled mess of formatting. Can you please reformat your additional code to match the existing formatting?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@atanisoft Fixed. Please kindly check it. Thank you.

size_t sent_bytes = 0;
for (size_t id_data = 0; id_data < size; id_data+=1000)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of a for loop using fixed sizes for chunks, please consider a loop similar to below (note this is not tested):

while(sent_bytes < size) {
  size_t sent = _client->write(&payload[sent_bytes], std::min(size - sent_bytes, _chunkSize));
  if (sent < 0) {
    log_e("Failed to send chunk!");
    break;
  }
  sent_bytes += sent;
}

Note that there is ZERO guarantee that the write call will send the full block size you request it to send thus you must check that it actually sent data and only then move the data starting point. The check for negative sent byte count is to check for sending errors.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@atanisoft Fixed. Please kindly check it. Thank you.

{
sent_bytes = _client->write(&payload[id_data], ((id_data+1000)<=size)?1000:(size-id_data));
log_d("Sent %u bytes\n", sent_bytes);
}
}
}

code = handleHeaderResponse();
log_d("sendRequest code=%d\n", code);
Expand Down