Skip to content

Commit d1f5d75

Browse files
Replace form_type and header_type with pair_type derived type (#34)
* Replace form_type and header_type with pair_type derived type * Add functionality for sending files via HTTP POST requests. * Refactor the set_body procedure * add HTTP_PUT * Edit docstrings * Indentation * Move data/file.txt in example/ --------- Co-authored-by: milancurcic <caomaco@gmail.com>
1 parent eee5765 commit d1f5d75

File tree

15 files changed

+301
-218
lines changed

15 files changed

+301
-218
lines changed

example/data/file.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Here is the data from the file that needs to be sent to the server.

example/post.f90

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
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, header_type
4+
use http, only: response_type, request, HTTP_POST, pair_type
55
implicit none
66
type(response_type) :: response
77
character(:), allocatable :: json_data
8-
type(header_type), allocatable :: req_header(:)
8+
type(pair_type), allocatable :: req_header(:)
99

10-
req_header = [header_type('Content-Type', 'application/json')]
10+
! Storing request header in array of pair_type object
11+
req_header = [pair_type('Content-Type', 'application/json')]
1112

1213
! JSON data we want to send
1314
json_data = '{"name":"Jhon","role":"developer"}'

example/post_file.f90

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
program post_file
2+
3+
! This program demonstrates sending File using POST request.
4+
5+
use http, only : request, response_type, HTTP_POST, pair_type
6+
implicit none
7+
type(response_type) :: response
8+
type(pair_type) :: file_data
9+
10+
! pair_type('<file_field_name>', '/path/to/file.txt')
11+
file_data = pair_type('file.txt', './example/data/file.txt')
12+
13+
response = request(url='https://httpbin.org/post', method=HTTP_POST, file=file_data)
14+
15+
if(.not. response%ok) then
16+
print *,'Error message : ', response%err_msg
17+
else
18+
print *, 'Response Code : ', response%status_code
19+
print *, 'Response Length : ', response%content_length
20+
print *, 'Response Method : ', response%method
21+
print *, 'Response Content : ', response%content
22+
end if
23+
end program post_file

example/post_form_data.f90

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
program post_form_data
2-
! This program demonstrates sending Form 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, header_type, form_type
2+
! This program demonstrates sending Form data using POST request and printing
3+
! the status, length of the body, method, and the body of the response.
4+
use http, only: response_type, request, HTTP_POST, pair_type
55
implicit none
66
type(response_type) :: response
7-
type(header_type), allocatable :: req_header(:)
8-
type(form_type), allocatable :: form_data(:)
7+
type(pair_type), allocatable :: req_header(:)
8+
type(pair_type), allocatable :: form_data(:)
99

10-
form_data = [form_type('param1', 'value1'), form_type('param2', 'value2')]
10+
! Storing form data in a array of pair_type object, each pair_type object
11+
! represent a single form field
12+
form_data = [pair_type('param1', 'value1'), pair_type('param2', 'value2')]
1113

1214
response = request(url='https://httpbin.org/post', method=HTTP_POST, form=form_data)
1315

example/response_header.f90

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@ program response_header
22
! This program demonstrates sending user-provided headers in a GET request
33
! and iterating over the headers of the response sent back by the server.
44
use stdlib_string_type, only: string_type, write(formatted)
5-
use http, only: response_type, request, header_type
5+
use http, only: response_type, request, pair_type
66

77
implicit none
88
type(response_type) :: response
9-
type(header_type), allocatable :: header(:), req_header(:)
9+
type(pair_type), allocatable :: header(:), req_header(:)
1010
character(:), allocatable :: val
1111
integer :: i = 0
1212

13+
! Storing request header in array of pair_type object, where each pair_type
14+
! object represents a single header. (in header-name,header-value format)
1315
req_header = [ &
14-
header_type('Another-One', 'Hello'), &
15-
header_type('Set-Cookie', 'Theme-Light'), &
16-
header_type('Set-Cookie', 'Auth-Token: 12345'), &
17-
header_type('User-Agent', 'my user agent') &
18-
]
16+
pair_type('Another-One', 'Hello'), &
17+
pair_type('Set-Cookie', 'Theme-Light'), &
18+
pair_type('Set-Cookie', 'Auth-Token: 12345'), &
19+
pair_type('User-Agent', 'my user agent') &
20+
]
1921

2022
response = request(url='https://httpbin.org/get', header=req_header)
2123

@@ -28,7 +30,7 @@ program response_header
2830
header = response%header
2931
! Iterate over response headers.
3032
do i = 1, size(header)
31-
print *, header(i)%key, ': ', header(i)%value
33+
print *, header(i)%name, ': ', header(i)%value
3234
end do
3335

3436
! getting header value by header name

src/http.f90

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
module http
22
use http_request, only: &
3-
HTTP_DELETE, HTTP_GET, HTTP_HEAD, HTTP_PATCH, HTTP_POST, HTTP_POST
3+
HTTP_DELETE, HTTP_GET, HTTP_HEAD, HTTP_PATCH, HTTP_POST, HTTP_PUT
44
use http_response, only: response_type
55
use http_client, only: request
6-
use http_header, only : header_type
7-
use http_form, only : form_type
6+
use http_pair, only : pair_type
87
end module http

0 commit comments

Comments
 (0)