Skip to content

Commit e172677

Browse files
Merge pull request #31 from rajkumardongre/json_to_data
Rename json -> data and remove JSON-specific content-type setting
2 parents 691ad82 + 5234861 commit e172677

File tree

4 files changed

+17
-22
lines changed

4 files changed

+17
-22
lines changed

example/post.f90

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
program post_request
22
! This program demonstrates sending JSON data using POST request and printing the
33
! status, length of the body, method, and the body of the response.
4-
use http, only: response_type, request, HTTP_POST
4+
use http, only: response_type, request, HTTP_POST, header_type
55
implicit none
66
type(response_type) :: response
77
character(:), allocatable :: json_data
8+
type(header_type), allocatable :: req_header(:)
9+
10+
req_header = [header_type('Content-Type', 'applicaiton/json')]
811

912
! JSON data we want to send
1013
json_data = '{"name":"Jhon","role":"developer"}'
1114

12-
response = request(url='https://httpbin.org/post', method=HTTP_POST, json=json_data)
15+
response = request(url='https://httpbin.org/post', method=HTTP_POST, data=json_data, header=req_header)
1316

1417
if(.not. response%ok) then
1518
print *,'Error message : ', response%err_msg

src/http/http_client.f90

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ module http_client
3535

3636
contains
3737
! Constructor for request_type type.
38-
function new_request(url, method, header, json) result(response)
38+
function new_request(url, method, header, data) result(response)
3939
integer, intent(in), optional :: method
4040
character(len=*), intent(in) :: url
41-
character(len=*), intent(in), optional :: json
41+
character(len=*), intent(in), optional :: data
4242
type(header_type), intent(in), optional :: header(:)
4343
type(request_type) :: request
4444
type(response_type) :: response
@@ -62,9 +62,9 @@ function new_request(url, method, header, json) result(response)
6262
request%header = [header_type('user-agent', 'fortran-http/0.1.0')]
6363
end if
6464

65-
if(present(json)) then
66-
request%json = json
67-
request%header = [request%header, header_type('Content-Type', 'application/json')]
65+
! setting the request data to be send
66+
if(present(data)) then
67+
request%data = data
6868
end if
6969

7070
! Populates the response
@@ -111,7 +111,7 @@ & function failed. This can occur due to insufficient memory available in the sy
111111
rc = set_method(curl_ptr, this%request%method, response)
112112

113113
! setting request body
114-
rc = set_body(curl_ptr, this%request%json)
114+
rc = set_body(curl_ptr, this%request%data)
115115

116116
! setting request header
117117
rc = curl_easy_setopt(curl_ptr, CURLOPT_HTTPHEADER, header_list_ptr);
@@ -187,15 +187,12 @@ function set_method(curl_ptr, method, response) result(status)
187187
end select
188188
end function set_method
189189

190-
function set_body(curl_ptr, json) result(status)
190+
function set_body(curl_ptr, data) result(status)
191191
type(c_ptr), intent(out) :: curl_ptr
192-
character(*), intent(in) :: json
193-
integer :: status, json_length
194-
json_length = len(json)
195-
! if(json_length > 0) then
196-
status = curl_easy_setopt(curl_ptr, CURLOPT_POSTFIELDS, json)
197-
status = curl_easy_setopt(curl_ptr, CURLOPT_POSTFIELDSIZE_LARGE, json_length)
198-
! end if
192+
character(*), intent(in) :: data
193+
integer :: status
194+
status = curl_easy_setopt(curl_ptr, CURLOPT_POSTFIELDS, data)
195+
status = curl_easy_setopt(curl_ptr, CURLOPT_POSTFIELDSIZE_LARGE, len(data))
199196
end function set_body
200197

201198
function client_response_callback(ptr, size, nmemb, client_data) bind(c)

src/http/http_request.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module http_request
1818

1919
! Request Type
2020
type :: request_type
21-
character(len=:), allocatable :: url, json
21+
character(len=:), allocatable :: url, data
2222
integer :: method
2323
type(header_type), allocatable :: header(:)
2424
end type request_type

test/test_get.f90

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ program test_get
4444
print '(a)', 'Failed : Status Code Validation'
4545
fail_test_case = fail_test_case + 1
4646
else
47-
print '(a)', 'Passed : Status Code Validation'
4847
passed_test_case = passed_test_case + 1
4948
end if
5049

@@ -55,7 +54,6 @@ program test_get
5554
print '(a)', 'Failed : Content Length Validation'
5655
fail_test_case = fail_test_case + 1
5756
else
58-
print '(a)', 'Passed : Content Length Validation'
5957
passed_test_case = passed_test_case + 1
6058
end if
6159

@@ -65,7 +63,6 @@ program test_get
6563
print '(a)', 'Failed : Content Validation'
6664
fail_test_case = fail_test_case + 1
6765
else
68-
print '(a)', 'Passed : Content Validation'
6966
passed_test_case = passed_test_case + 1
7067
end if
7168

@@ -75,7 +72,6 @@ program test_get
7572
print '(a)', 'Failed : Header Size Validation'
7673
fail_test_case = fail_test_case + 1
7774
else
78-
print '(a)', 'Passed : Header Size Validation'
7975
passed_test_case = passed_test_case + 1
8076
end if
8177

@@ -85,7 +81,6 @@ program test_get
8581
print '(a)', 'Failed : Header Value Validation'
8682
fail_test_case = fail_test_case + 1
8783
else
88-
print '(a)', 'Passed : Header Value Validation'
8984
passed_test_case = passed_test_case + 1
9085
end if
9186

0 commit comments

Comments
 (0)