added support for data in POST request

This commit is contained in:
Jocelyn Fiat
2011-10-06 19:09:17 +02:00
parent 720d8be08a
commit e5eb11b4e7
4 changed files with 64 additions and 4 deletions

View File

@@ -38,6 +38,10 @@ feature -- Access
form_data_parameters: HASH_TABLE [READABLE_STRING_32, READABLE_STRING_8] form_data_parameters: HASH_TABLE [READABLE_STRING_32, READABLE_STRING_8]
upload_data: detachable READABLE_STRING_8
upload_filename: detachable READABLE_STRING_8
feature -- Status report feature -- Status report
has_form_data: BOOLEAN has_form_data: BOOLEAN
@@ -45,6 +49,16 @@ feature -- Status report
Result := not form_data_parameters.is_empty Result := not form_data_parameters.is_empty
end end
has_upload_data: BOOLEAN
do
Result := attached upload_data as d and then not d.is_empty
end
has_upload_filename: BOOLEAN
do
Result := attached upload_filename as fn and then not fn.is_empty
end
feature -- Element change feature -- Element change
add_query_parameter (k: READABLE_STRING_8; v: READABLE_STRING_32) add_query_parameter (k: READABLE_STRING_8; v: READABLE_STRING_32)
@@ -62,4 +76,18 @@ feature -- Element change
credentials_required := b credentials_required := b
end end
set_upload_data (a_data: like upload_data)
require
has_no_upload_data: not has_upload_data
do
upload_data := a_data
end
set_upload_filename (a_fn: like upload_filename)
require
has_no_upload_filename: not has_upload_filename
do
upload_filename := a_fn
end
end end

View File

@@ -41,11 +41,11 @@ feature -- Basic operation
deferred deferred
end end
post (a_path: READABLE_STRING_8; ctx: detachable HTTP_CLIENT_REQUEST_CONTEXT): HTTP_CLIENT_RESPONSE post (a_path: READABLE_STRING_8; ctx: detachable HTTP_CLIENT_REQUEST_CONTEXT; data: detachable READABLE_STRING_8): HTTP_CLIENT_RESPONSE
deferred deferred
end end
put (a_path: READABLE_STRING_8; ctx: detachable HTTP_CLIENT_REQUEST_CONTEXT): HTTP_CLIENT_RESPONSE put (a_path: READABLE_STRING_8; ctx: detachable HTTP_CLIENT_REQUEST_CONTEXT; fn: detachable READABLE_STRING_8): HTTP_CLIENT_RESPONSE
deferred deferred
end end

View File

@@ -142,6 +142,22 @@ feature -- Execution
curl_easy.setopt_form (curl_handle, {CURL_OPT_CONSTANTS}.curlopt_httppost, l_form) curl_easy.setopt_form (curl_handle, {CURL_OPT_CONSTANTS}.curlopt_httppost, l_form)
end end
end end
if ctx /= Void then
if
request_method.is_case_insensitive_equal ("POST") and then
ctx.has_upload_data and then attached ctx.upload_data as l_upload_data
then
curl_easy.setopt_string (curl_handle, {CURL_OPT_CONSTANTS}.curlopt_postfields, l_upload_data)
curl_easy.setopt_integer (curl_handle, {CURL_OPT_CONSTANTS}.curlopt_postfieldsize, l_upload_data.count)
elseif
request_method.is_case_insensitive_equal ("PUT") and then
ctx.has_upload_filename and then attached ctx.upload_filename as l_upload_filename
then
-- curl_easy.setopt_string (curl_handle, {CURL_OPT_CONSTANTS}.curlopt_postfields, l_upload_data)
-- curl_easy.setopt_integer (curl_handle, {CURL_OPT_CONSTANTS}.curlopt_postfieldsize, l_upload_data.count)
--| Not Yet Implemented
end
end
curl.global_init curl.global_init

View File

@@ -39,19 +39,35 @@ feature -- Basic operation
Result := execute_request (req, ctx) Result := execute_request (req, ctx)
end end
post (a_path: READABLE_STRING_8; ctx: detachable HTTP_CLIENT_REQUEST_CONTEXT): HTTP_CLIENT_RESPONSE post (a_path: READABLE_STRING_8; a_ctx: detachable HTTP_CLIENT_REQUEST_CONTEXT; a_data: detachable READABLE_STRING_8): HTTP_CLIENT_RESPONSE
local local
req: HTTP_CLIENT_REQUEST req: HTTP_CLIENT_REQUEST
ctx: detachable HTTP_CLIENT_REQUEST_CONTEXT
do do
create {LIBCURL_HTTP_CLIENT_REQUEST} req.make (base_url + a_path, "POST", Current) create {LIBCURL_HTTP_CLIENT_REQUEST} req.make (base_url + a_path, "POST", Current)
ctx := a_ctx
if a_data /= Void then
if ctx = Void then
create ctx.make
end
ctx.set_upload_data (a_data)
end
Result := execute_request (req, ctx) Result := execute_request (req, ctx)
end end
put (a_path: READABLE_STRING_8; ctx: detachable HTTP_CLIENT_REQUEST_CONTEXT): HTTP_CLIENT_RESPONSE put (a_path: READABLE_STRING_8; a_ctx: detachable HTTP_CLIENT_REQUEST_CONTEXT; fn: detachable READABLE_STRING_8): HTTP_CLIENT_RESPONSE
local local
req: HTTP_CLIENT_REQUEST req: HTTP_CLIENT_REQUEST
ctx: detachable HTTP_CLIENT_REQUEST_CONTEXT
do do
create {LIBCURL_HTTP_CLIENT_REQUEST} req.make (base_url + a_path, "PUT", Current) create {LIBCURL_HTTP_CLIENT_REQUEST} req.make (base_url + a_path, "PUT", Current)
ctx := a_ctx
if fn /= Void then
if ctx = Void then
create ctx.make
end
ctx.set_upload_filename (fn)
end
Result := execute_request (req, ctx) Result := execute_request (req, ctx)
end end