Skip to content

Commit 478aa06

Browse files
Merge pull request #27 from rajkumardongre/post_json
sending json using post request
2 parents 477b222 + 58f9a62 commit 478aa06

File tree

5 files changed

+59
-13
lines changed

5 files changed

+59
-13
lines changed

example/simple_get.f90 renamed to example/get.f90

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
program simple_get
1+
program get_request
22
! This program demonstrates sending a simple GET request and printing the
33
! status, length of the body, method, and the body of the response.
44
use http, only: response_type, request
@@ -15,4 +15,4 @@ program simple_get
1515
print *, 'Response Content : ', response%content
1616
end if
1717

18-
end program simple_get
18+
end program get_request

example/post.f90

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
program post_request
2+
! This program demonstrates sending JSON data using POST request and printing the
3+
! status, length of the body, method, and the body of the response.
4+
use http, only: response_type, request, HTTP_POST
5+
implicit none
6+
type(response_type) :: response
7+
character(:), allocatable :: json_data
8+
9+
! JSON data we want to send
10+
json_data = '{"name":"Jhon","role":"developer"}'
11+
12+
response = request(url='https://httpbin.org/post', method=HTTP_POST, json=json_data)
13+
14+
if(.not. response%ok) then
15+
print *,'Error message : ', response%err_msg
16+
else
17+
print *, 'Response Code : ', response%status_code
18+
print *, 'Response Length : ', response%content_length
19+
print *, 'Response Method : ', response%method
20+
print *, 'Response Content : ', response%content
21+
end if
22+
23+
end program post_request

example/response_header.f90

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ program response_header
1717
header_type('User-Agent', 'my user agent') &
1818
]
1919

20-
response = request(url='https://reqres.in/api/users/1', header=req_header)
20+
response = request(url='https://httpbin.org/get', header=req_header)
2121

2222
if (.not. response%ok) then
2323
print *,'Error message : ', response%err_msg
2424
else
25+
print*, '::::::::::::::::: Request send by us :::::::::::::::::'
26+
print *, response%content
2527
print*, '::::::::::::::::: Fetching all response header :::::::::::::::::'
2628
header = response%header
2729
! Iterate over response headers.

src/http/http_client.f90

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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

3536
contains
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

src/http/http_request.f90

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

1717
! Request Type
1818
type :: request_type
19-
character(len=:), allocatable :: url
19+
character(len=:), allocatable :: url, json
2020
integer :: method
2121
type(header_type), allocatable :: header(:)
2222
end type request_type

0 commit comments

Comments
 (0)