@@ -6,7 +6,8 @@ module http_client
66 curl_easy_strerror, curl_slist_append, CURLE_OK, &
77 CURLINFO_RESPONSE_CODE, CURLOPT_CUSTOMREQUEST, CURLOPT_HEADERDATA, &
88 CURLOPT_HEADERFUNCTION, CURLOPT_HTTPHEADER, CURLOPT_URL, &
9- CURLOPT_WRITEDATA, CURLOPT_WRITEFUNCTION
9+ CURLOPT_WRITEDATA, CURLOPT_WRITEFUNCTION, &
10+ CURLOPT_POSTFIELDS, CURLOPT_POSTFIELDSIZE_LARGE
1011 use stdlib_optval, only: optval
1112 use http_request, only: request_type
1213 use http_response, only: response_type
@@ -34,14 +35,18 @@ module http_client
3435
3536contains
3637 ! Constructor for request_type type.
37- function new_request (url , method , header ) result(response)
38- character (len=* ), intent (in ) :: url
38+ function new_request (url , method , header , json ) result(response)
3939 integer , intent (in ), optional :: method
40+ character (len=* ), intent (in ) :: url
41+ character (len=* ), intent (in ), optional :: json
4042 type (header_type), intent (in ), optional :: header(:)
4143 type (request_type) :: request
4244 type (response_type) :: response
4345 type (client_type) :: client
4446
47+ ! setting request url
48+ request% url = url
49+
4550 ! Set default HTTP method.
4651 request% method = optval(method, 1 )
4752
@@ -50,9 +55,11 @@ function new_request(url, method, header) result(response)
5055 if (present (header)) then
5156 request% header = [header, request% header]
5257 end if
53-
54- ! setting request url
55- request% url = url
58+
59+ if (present (json)) then
60+ request% json = json
61+ request% header = [request% header, header_type(' Content-Type' , ' application/json' )]
62+ end if
5663
5764 client = client_type(request= request)
5865
@@ -96,7 +103,10 @@ & function failed. This can occur due to insufficient memory available in the sy
96103 rc = curl_easy_setopt(curl_ptr, CURLOPT_URL, this% request% url // c_null_char)
97104
98105 ! setting request method
99- rc = client_set_method(curl_ptr, this% request% method, response)
106+ rc = set_method(curl_ptr, this% request% method, response)
107+
108+ ! setting request body
109+ rc = set_body(curl_ptr, this% request% json)
100110
101111 ! setting request header
102112 rc = curl_easy_setopt(curl_ptr, CURLOPT_HTTPHEADER, header_list_ptr);
@@ -142,7 +152,7 @@ subroutine prepare_request_header_ptr(header_list_ptr, req_headers)
142152 end do
143153 end subroutine prepare_request_header_ptr
144154
145- function client_set_method (curl_ptr , method , response ) result(status)
155+ function set_method (curl_ptr , method , response ) result(status)
146156 type (c_ptr), intent (out ) :: curl_ptr
147157 integer , intent (in ) :: method
148158 type (response_type), intent (out ) :: response
@@ -170,7 +180,18 @@ function client_set_method(curl_ptr, method, response) result(status)
170180 case default
171181 error stop ' Method argument can be either HTTP_GET, HTTP_HEAD, HTTP_POST, HTTP_PUT, HTTP_DELETE, HTTP_PATCH'
172182 end select
173- end function client_set_method
183+ end function set_method
184+
185+ function set_body (curl_ptr , json ) result(status)
186+ type (c_ptr), intent (out ) :: curl_ptr
187+ character (* ), intent (in ) :: json
188+ integer :: status, json_length
189+ json_length = len (json)
190+ ! if(json_length > 0) then
191+ status = curl_easy_setopt(curl_ptr, CURLOPT_POSTFIELDS, json)
192+ status = curl_easy_setopt(curl_ptr, CURLOPT_POSTFIELDSIZE_LARGE, json_length)
193+ ! end if
194+ end function set_body
174195
175196 function client_response_callback (ptr , size , nmemb , client_data ) bind(c)
176197 type (c_ptr), intent (in ), value :: ptr
0 commit comments