From ed5c807c99df9590c7883502d8e152fad6849e9b Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 27 Sep 2021 01:40:21 +0200 Subject: [PATCH] LibWeb: Allow passing a (String) body to XMLHttpRequest.send() This patch implements the simplest form of send(body): strings. --- Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp | 4 +++- Userland/Libraries/LibWeb/XHR/XMLHttpRequest.h | 2 +- Userland/Libraries/LibWeb/XHR/XMLHttpRequest.idl | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp index f64a58d12c..73258099aa 100644 --- a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp +++ b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp @@ -157,7 +157,7 @@ DOM::ExceptionOr XMLHttpRequest::open(const String& method, const String& } // https://xhr.spec.whatwg.org/#dom-xmlhttprequest-send -DOM::ExceptionOr XMLHttpRequest::send() +DOM::ExceptionOr XMLHttpRequest::send(String const& body) { if (m_ready_state != ReadyState::Opened) return DOM::InvalidStateError::create("XHR readyState is not OPENED"); @@ -188,6 +188,8 @@ DOM::ExceptionOr XMLHttpRequest::send() auto request = LoadRequest::create_for_url_on_page(request_url, m_window->page()); request.set_method(m_method); + if (!body.is_null()) + request.set_body(body.to_byte_buffer()); for (auto& it : m_request_headers) request.set_header(it.key, it.value); diff --git a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.h b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.h index d33bf48c7f..ae47e556cc 100644 --- a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.h +++ b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.h @@ -52,7 +52,7 @@ public: String response_text() const; DOM::ExceptionOr open(const String& method, const String& url); - DOM::ExceptionOr send(); + DOM::ExceptionOr send(String const& body); DOM::ExceptionOr set_request_header(const String& header, const String& value); diff --git a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.idl b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.idl index 569b3f00b3..9e1e0ec14c 100644 --- a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.idl +++ b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.idl @@ -14,7 +14,7 @@ interface XMLHttpRequest : XMLHttpRequestEventTarget { undefined open(DOMString method, DOMString url); undefined setRequestHeader(DOMString name, DOMString value); - undefined send(); + undefined send(optional USVString body = {}); ByteString? getResponseHeader(ByteString name); ByteString getAllResponseHeaders();