jpbarrette/curlpp

Duplicate messages http post

benyaminakdemir opened this issue · 1 comments

Hi, there seems to be a problem with duplicate messages getting generated when sending a single HTTP POST. The screenshot shows the network traffic (Wireshark) after calling the function postJson

std::string HTTPClient::postJson(const std::string& url, const std::string& json)
{
	auto write_data = [](char* ptr, size_t size, size_t nmemb) -> size_t {  return size * nmemb;  };
	std::string res;
	try {
		curlpp::Cleanup cleaner;
		curlpp::Easy request;
	
		std::list<std::string> header;
		header.push_back("Content-Type: application/json");

		request.setOpt(new curlpp::options::HttpHeader(header));
		request.setOpt(new curlpp::options::Url(url));
		request.setOpt(new curlpp::options::PostFieldSize(json.length()));
		request.setOpt(new curlpp::options::PostFields(json));
		request.setOpt(new curlpp::options::WriteFunction(write_data)); //not in use 
		request.setOpt(new curlpp::options::UserAgent("curl/7.38.0"));
		request.perform();

		std::stringstream ss;
		ss << request;
		res = ss.str();
	}
	catch (curlpp::LogicError& e) {
		std::cout << e.what() << std::endl;
	}
	catch (curlpp::RuntimeError& e) {
		std::cout << e.what() << std::endl;
	}
	return res;
}

multiple_post

request.perform() writes a stream out, and then you write the stream again in:

	std::stringstream ss;
	ss << request;
	res = ss.str();

Drop it and no more double sending will take place. I've had this problem myself