@@ -19,7 +19,8 @@ module http_client
1919 CURLOPT_WRITEDATA, CURLOPT_WRITEFUNCTION, &
2020 CURLOPT_POSTFIELDS, CURLOPT_POSTFIELDSIZE_LARGE, curl_easy_escape, &
2121 curl_mime_init, curl_mime_addpart, curl_mime_filedata,curl_mime_name, &
22- CURLOPT_MIMEPOST,curl_mime_data, CURL_ZERO_TERMINATED
22+ CURLOPT_MIMEPOST,curl_mime_data, CURL_ZERO_TERMINATED, &
23+ CURLOPT_HTTPAUTH, CURLAUTH_BASIC, CURLOPT_USERNAME, CURLOPT_PASSWORD
2324 use stdlib_optval, only: optval
2425 use http_request, only: request_type
2526 use http_response, only: response_type
@@ -53,7 +54,7 @@ module http_client
5354 ! new client_type object using the request object as a parameter and sends the request to the server
5455 ! using the client_get_response method. The function returns the response_type object containing the
5556 ! server's response.
56- function new_request (url , method , header , data , form , file ) result(response)
57+ function new_request (url , method , header , data , form , file , auth ) result(response)
5758 ! ! This function creates a new HTTP request object of the request_type type and sends
5859 ! ! the request to the server using the client_type object. The function takes the URL,
5960 ! ! HTTP method, request headers, request data, and form data as input arguments and returns
@@ -71,6 +72,8 @@ function new_request(url, method, header, data, form, file) result(response)
7172 ! ! An optional array of pair_type objects that specifies the form data to send in the request body.
7273 type (pair_type), intent (in ), optional :: file
7374 ! ! An optional pair_type object that specifies the file data to send in the request body.
75+ type (pair_type), intent (in ), optional :: auth
76+ ! ! An optional pair_type object that stores the username and password for Authentication
7477 type (response_type) :: response
7578 ! ! A response_type object containing the server's response.
7679 type (request_type) :: request
@@ -109,6 +112,11 @@ function new_request(url, method, header, data, form, file) result(response)
109112 request% file = file
110113 end if
111114
115+ ! setting username and password for Authentication
116+ if (present (auth)) then
117+ request% auth = auth
118+ end if
119+
112120 ! Populates the response
113121 client = client_type(request= request)
114122 response = client% client_get_response()
@@ -163,6 +171,9 @@ & function failed. This can occur due to insufficient memory available in the sy
163171 ! setting request body
164172 rc = set_body(curl_ptr, this% request)
165173
174+ ! setting request authentication
175+ rc = set_auth(curl_ptr, this% request)
176+
166177 ! prepare headers for curl
167178 call prepare_request_header_ptr(header_list_ptr, this% request% header)
168179
@@ -315,7 +326,6 @@ function set_body(curl_ptr, request) result(status)
315326 ! ! An integer value representing the status of the curl_easy_setopt function call.
316327
317328 integer :: i
318- character (len= :), allocatable :: form_encoded_str
319329 type (c_ptr) :: mime_ptr, part_ptr
320330
321331 ! if only data is passed
@@ -374,6 +384,26 @@ function set_postfields(curl_ptr, data) result(status)
374384
375385 end function set_postfields
376386
387+ function set_auth (curl_ptr , request ) result(status)
388+ ! ! Set the user name and password for Authentication. It sends the user name
389+ ! ! and password over the network in plain text, easily captured by others.
390+ type (c_ptr), intent (out ) :: curl_ptr
391+ ! ! An out argument of type c_ptr that is set to point to a new curl handle.
392+ type (request_type), intent (inout ) :: request
393+ ! ! The HTTP request
394+ integer :: status
395+ ! ! An integer value representing the status of the curl_easy_setopt function call.
396+
397+ if (allocated (request% auth)) then
398+ status = curl_easy_setopt(curl_ptr, CURLOPT_HTTPAUTH, CURLAUTH_BASIC)
399+ status = curl_easy_setopt(curl_ptr, CURLOPT_USERNAME, request% auth% name)
400+ status = curl_easy_setopt(curl_ptr, CURLOPT_PASSWORD, request% auth% value)
401+ else
402+ ! No curl function was called so set status to zero.
403+ status = 0
404+ end if
405+ end function set_auth
406+
377407 ! This function is a callback function used by the libcurl library to handle HTTP responses. It is
378408 ! called for each chunk of data received from the server and appends the data to a response_type object.
379409 ! The function takes four input arguments: ptr, size, nmemb, and client_data. ptr is a pointer to the
0 commit comments