diff --git a/components/winhttp/network_fetcher.cc b/components/winhttp/network_fetcher.cc index 1ec77d9d0c46d..10b3922f2d9f2 100644 --- a/components/winhttp/network_fetcher.cc +++ b/components/winhttp/network_fetcher.cc @@ -267,9 +267,14 @@ HRESULT NetworkFetcher::SendRequest(const std::string& data) { VLOG(2) << data; - const uint32_t bytes_to_send = base::saturated_cast<uint32_t>(data.size()); - void* request_body = - bytes_to_send ? const_cast<char*>(data.c_str()) : WINHTTP_NO_REQUEST_DATA; + // Make a copy of the request data to ensure the buffer is available until + // the request is processed. + request_data_ = data; + + const uint32_t bytes_to_send = + base::saturated_cast<uint32_t>(request_data_.size()); + void* request_body = bytes_to_send ? const_cast<char*>(request_data_.c_str()) + : WINHTTP_NO_REQUEST_DATA; if (!::WinHttpSendRequest(request_handle_.get(), WINHTTP_NO_ADDITIONAL_HEADERS, 0, request_body, bytes_to_send, bytes_to_send, context())) { diff --git a/components/winhttp/network_fetcher.h b/components/winhttp/network_fetcher.h index db8fcc1ccdfa4..001a1350448ae 100644 --- a/components/winhttp/network_fetcher.h +++ b/components/winhttp/network_fetcher.h @@ -133,6 +133,7 @@ class NetworkFetcher : public base::RefCountedThreadSafe<NetworkFetcher> { std::string path_for_request_; base::WStringPiece verb_; + std::string request_data_; // The value of Content-Type header, e.g. "application/json". std::string content_type_; WriteDataCallback write_data_callback_;