From 108c2f4a80c2926d75ee3f53352410a63e7dfcb4 Mon Sep 17 00:00:00 2001 From: Rajkumar Date: Tue, 20 Jun 2023 11:50:13 +0530 Subject: [PATCH 1/2] Rename json -> data and remove JSON-specific content-type setting --- example/post.f90 | 7 +++++-- src/http/http_client.f90 | 23 +++++++++++------------ src/http/http_request.f90 | 2 +- test/test_get.f90 | 5 ----- 4 files changed, 17 insertions(+), 20 deletions(-) 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..4dfb541 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,8 @@ 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')] + if(present(data)) then + request%data = data end if ! Populates the response @@ -111,7 +110,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,14 +186,14 @@ 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) + character(*), intent(in) :: data + integer :: status, data_length + data_length = len(data) ! 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) + status = curl_easy_setopt(curl_ptr, CURLOPT_POSTFIELDS, data) + status = curl_easy_setopt(curl_ptr, CURLOPT_POSTFIELDSIZE_LARGE, data_length) ! end if end function set_body 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 From 5234861b2f8d7dead2696e9964b9d8561443a7e1 Mon Sep 17 00:00:00 2001 From: Rajkumar Date: Tue, 20 Jun 2023 20:24:18 +0530 Subject: [PATCH 2/2] use len(data) instead of data_length --- src/http/http_client.f90 | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/http/http_client.f90 b/src/http/http_client.f90 index 4dfb541..e0c5a34 100644 --- a/src/http/http_client.f90 +++ b/src/http/http_client.f90 @@ -62,6 +62,7 @@ function new_request(url, method, header, data) result(response) request%header = [header_type('user-agent', 'fortran-http/0.1.0')] end if + ! setting the request data to be send if(present(data)) then request%data = data end if @@ -189,12 +190,9 @@ end function set_method function set_body(curl_ptr, data) result(status) type(c_ptr), intent(out) :: curl_ptr character(*), intent(in) :: data - integer :: status, data_length - data_length = len(data) - ! if(json_length > 0) then - status = curl_easy_setopt(curl_ptr, CURLOPT_POSTFIELDS, data) - status = curl_easy_setopt(curl_ptr, CURLOPT_POSTFIELDSIZE_LARGE, data_length) - ! end if + 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)