diff --git a/example/post.f90 b/example/post.f90 index f31b541..5df254d 100644 --- a/example/post.f90 +++ b/example/post.f90 @@ -1,15 +1,18 @@ program post_request ! This program demonstrates sending JSON data using POST request and printing the ! status, length of the body, method, and the body of the response. - use http, only: response_type, request, HTTP_POST + use http, only: response_type, request, HTTP_POST, header_type implicit none type(response_type) :: response character(:), allocatable :: json_data + type(header_type), allocatable :: req_header(:) + + req_header = [header_type('Content-Type', 'applicaiton/json')] ! JSON data we want to send json_data = '{"name":"Jhon","role":"developer"}' - response = request(url='https://httpbin.org/post', method=HTTP_POST, json=json_data) + response = request(url='https://httpbin.org/post', method=HTTP_POST, data=json_data, header=req_header) if(.not. response%ok) then print *,'Error message : ', response%err_msg diff --git a/src/http/http_client.f90 b/src/http/http_client.f90 index ab815e8..e0c5a34 100644 --- a/src/http/http_client.f90 +++ b/src/http/http_client.f90 @@ -35,10 +35,10 @@ module http_client contains ! Constructor for request_type type. - function new_request(url, method, header, json) result(response) + function new_request(url, method, header, data) result(response) integer, intent(in), optional :: method character(len=*), intent(in) :: url - character(len=*), intent(in), optional :: json + character(len=*), intent(in), optional :: data type(header_type), intent(in), optional :: header(:) type(request_type) :: request type(response_type) :: response @@ -62,9 +62,9 @@ function new_request(url, method, header, json) result(response) request%header = [header_type('user-agent', 'fortran-http/0.1.0')] end if - if(present(json)) then - request%json = json - request%header = [request%header, header_type('Content-Type', 'application/json')] + ! setting the request data to be send + if(present(data)) then + request%data = data end if ! Populates the response @@ -111,7 +111,7 @@ & function failed. This can occur due to insufficient memory available in the sy rc = set_method(curl_ptr, this%request%method, response) ! setting request body - rc = set_body(curl_ptr, this%request%json) + rc = set_body(curl_ptr, this%request%data) ! setting request header rc = curl_easy_setopt(curl_ptr, CURLOPT_HTTPHEADER, header_list_ptr); @@ -187,15 +187,12 @@ function set_method(curl_ptr, method, response) result(status) end select end function set_method - function set_body(curl_ptr, json) result(status) + function set_body(curl_ptr, data) result(status) type(c_ptr), intent(out) :: curl_ptr - character(*), intent(in) :: json - integer :: status, json_length - json_length = len(json) - ! if(json_length > 0) then - status = curl_easy_setopt(curl_ptr, CURLOPT_POSTFIELDS, json) - status = curl_easy_setopt(curl_ptr, CURLOPT_POSTFIELDSIZE_LARGE, json_length) - ! end if + character(*), intent(in) :: data + integer :: status + status = curl_easy_setopt(curl_ptr, CURLOPT_POSTFIELDS, data) + status = curl_easy_setopt(curl_ptr, CURLOPT_POSTFIELDSIZE_LARGE, len(data)) end function set_body function client_response_callback(ptr, size, nmemb, client_data) bind(c) diff --git a/src/http/http_request.f90 b/src/http/http_request.f90 index e5a1839..15c6ca5 100644 --- a/src/http/http_request.f90 +++ b/src/http/http_request.f90 @@ -18,7 +18,7 @@ module http_request ! Request Type type :: request_type - character(len=:), allocatable :: url, json + character(len=:), allocatable :: url, data integer :: method type(header_type), allocatable :: header(:) end type request_type diff --git a/test/test_get.f90 b/test/test_get.f90 index 1a537c4..165e3ec 100644 --- a/test/test_get.f90 +++ b/test/test_get.f90 @@ -44,7 +44,6 @@ program test_get print '(a)', 'Failed : Status Code Validation' fail_test_case = fail_test_case + 1 else - print '(a)', 'Passed : Status Code Validation' passed_test_case = passed_test_case + 1 end if @@ -55,7 +54,6 @@ program test_get print '(a)', 'Failed : Content Length Validation' fail_test_case = fail_test_case + 1 else - print '(a)', 'Passed : Content Length Validation' passed_test_case = passed_test_case + 1 end if @@ -65,7 +63,6 @@ program test_get print '(a)', 'Failed : Content Validation' fail_test_case = fail_test_case + 1 else - print '(a)', 'Passed : Content Validation' passed_test_case = passed_test_case + 1 end if @@ -75,7 +72,6 @@ program test_get print '(a)', 'Failed : Header Size Validation' fail_test_case = fail_test_case + 1 else - print '(a)', 'Passed : Header Size Validation' passed_test_case = passed_test_case + 1 end if @@ -85,7 +81,6 @@ program test_get print '(a)', 'Failed : Header Value Validation' fail_test_case = fail_test_case + 1 else - print '(a)', 'Passed : Header Value Validation' passed_test_case = passed_test_case + 1 end if