Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions example/data/file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Here is the data from the file that needs to be sent to the server.
7 changes: 4 additions & 3 deletions example/post.f90
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
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, header_type
use http, only: response_type, request, HTTP_POST, pair_type
implicit none
type(response_type) :: response
character(:), allocatable :: json_data
type(header_type), allocatable :: req_header(:)
type(pair_type), allocatable :: req_header(:)

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

! JSON data we want to send
json_data = '{"name":"Jhon","role":"developer"}'
Expand Down
23 changes: 23 additions & 0 deletions example/post_file.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
program post_file

! This program demonstrates sending File using POST request.

use http, only : request, response_type, HTTP_POST, pair_type
implicit none
type(response_type) :: response
type(pair_type) :: file_data

! pair_type('<file_field_name>', '/path/to/file.txt')
file_data = pair_type('file.txt', './example/data/file.txt')

response = request(url='https://httpbin.org/post', method=HTTP_POST, file=file_data)

if(.not. response%ok) then
print *,'Error message : ', response%err_msg
else
print *, 'Response Code : ', response%status_code
print *, 'Response Length : ', response%content_length
print *, 'Response Method : ', response%method
print *, 'Response Content : ', response%content
end if
end program post_file
14 changes: 8 additions & 6 deletions example/post_form_data.f90
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
program post_form_data
! This program demonstrates sending Form 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, header_type, form_type
! This program demonstrates sending Form 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, pair_type
implicit none
type(response_type) :: response
type(header_type), allocatable :: req_header(:)
type(form_type), allocatable :: form_data(:)
type(pair_type), allocatable :: req_header(:)
type(pair_type), allocatable :: form_data(:)

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

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

Expand Down
18 changes: 10 additions & 8 deletions example/response_header.f90
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@ program response_header
! This program demonstrates sending user-provided headers in a GET request
! and iterating over the headers of the response sent back by the server.
use stdlib_string_type, only: string_type, write(formatted)
use http, only: response_type, request, header_type
use http, only: response_type, request, pair_type

implicit none
type(response_type) :: response
type(header_type), allocatable :: header(:), req_header(:)
type(pair_type), allocatable :: header(:), req_header(:)
character(:), allocatable :: val
integer :: i = 0

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

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

Expand All @@ -28,7 +30,7 @@ program response_header
header = response%header
! Iterate over response headers.
do i = 1, size(header)
print *, header(i)%key, ': ', header(i)%value
print *, header(i)%name, ': ', header(i)%value
end do

! getting header value by header name
Expand Down
5 changes: 2 additions & 3 deletions src/http.f90
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
module http
use http_request, only: &
HTTP_DELETE, HTTP_GET, HTTP_HEAD, HTTP_PATCH, HTTP_POST, HTTP_POST
HTTP_DELETE, HTTP_GET, HTTP_HEAD, HTTP_PATCH, HTTP_POST, HTTP_PUT
use http_response, only: response_type
use http_client, only: request
use http_header, only : header_type
use http_form, only : form_type
use http_pair, only : pair_type
end module http
Loading