Skip to content

http_request body will be purged after calling http_client::request() #627

@evakili

Description

@evakili

It is a simple code to test this issue (or as designed feature):

#include <cpprest/http_client.h>
using namespace web::http;

int main() {
	try {
		auto client = client::http_client{ U("https://jsonplaceholder.typicode.com/") };
		auto request = http_request{ methods::POST };
		request.set_request_uri(U("/posts"));
		web::json::value requestData;
		requestData[L"title"] = web::json::value::string(U("hello!"));
		requestData[L"body"] = web::json::value::string(U("It is a salutation..."));
		requestData[L"userId"] = web::json::value::number(100);
		request.set_body(requestData);
		auto response = client.request(request).get();
		// here request has no body, but other attributes of it such as headers are remained unchanged
		std::wcout << response.status_code() << std::endl;
	}
	catch (std::exception& e) {
		std::cout << e.what() << std::endl;
	}
    return 0;
}

Why after calling http_client::request(http_request) the body is being emptied? The argument is passed by value and I expect no side effects on it. I guess that the root of this behavior is in using a shared istream (i.e. http_msg_base::m_inStream) between instances of copied http_request that its data is consumed in call to http_client::request(http_request). But it is confusing and against principle of least astonishment.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions