withProgressCallback

Hey im trying to create a callback function that will update an atomic<float> that will will reference the percentage of the upload, but the callback is never being called:

url = url.withFileToUpload("file", userAudioFile, "audio/wav")
	.withParameter("token", token)
	.withParameter("bpm", String(globalVars.getBpm()))
	.withParameter("genre", globalVars.getGenere());
DBG("API URL: " + url.toString(true));

// Create input stream with progress tracking
auto progressCallback = [&globalVars](int bytesSent, int totalBytes) {
	DBG("Bytes sent: " + String(bytesSent) + " Total bytes: " + String(totalBytes)); 
//this is never printed
	if (totalBytes > 0)
	{
		float progress = static_cast<float>(bytesSent) / totalBytes;
		DBG("Raw progress: " + String(progress));
		globalVars.setUploadProgress(progress);
	}
	return true;
	};


URL::InputStreamOptions options(URL::ParameterHandling::inPostData);
options.withProgressCallback(progressCallback);

I think you are using a builder function wrong here. URL:: InputStreamOptions::withProgressCallback returns a copy of the options with that option added. However from looking at the code it seems like you didn’t use the return value and probably used the original options and not the modified copy.

Rewrite your code like:

auto options = URL::InputStreamOptions (URL::ParameterHandling::inPostData)
    .withProgressCallback (progressCallback);
2 Likes

That worked! wow tnx

1 Like