fortran-lang / http-client

http-client offers a user-friendly, high-level API to make HTTP requests in Fortran.
https://http-client.fortran-lang.org/
MIT License
60 stars 8 forks source link

What is the best Practices for Passing Headers in HTTP Requests ? #20

Closed rajkumardongre closed 1 year ago

rajkumardongre commented 1 year ago

Hello, I have come up with an idea for passing headers to an HTTP request. My idea involves creating a derived type named header_type, below is it's defination :

type :: header_type
    type(c_ptr) :: header_list_ptr = c_null_ptr
    integer :: header_count = 0
contains
    procedure :: set_header
end type header_type

since curl maintains a linked list for storing the request header, header_list_ptr will point to the head of the linked list, header_count will maintain the total number of headers passed to the request. set_header subroutine will take the header key and value as input argument and store it inside the header linked list in the required format.

Here is an example of how this approach can be used to add headers to an HTTP request:

  use http, only: response_type, request, header_type
  implicit none
  type(response_type) :: response
  type(header_type) :: request_header

  ! Setting request header
  call request_header%set_header('h1', 'v1')
  call request_header%set_header('h2', 'v2')
  call request_header%set_header('h3', 'v3')

  response = request(url='https://jsonplaceholder.typicode.com/todos/1', header=request_header)

I have added an example of this approach in my recent commit for further insight. I would appreciate your thoughts and suggestions on this approach.

interkosmos commented 1 year ago

I recommend not to couple your architecture to closely with cURL. Ideally, you just call cURL in a single function or subroutine that translates your types to cURL. Your data structures should follow the HTTP protocol, not the public API of cURL. You don’t even need to store cURL pointers (curl_ptr, header_list_ptr) in your derived types, since they will be NULL once the request has been finished. This way, we are technically able to replace the backend in future (for instance, libsoup instead of cURL).

rajkumardongre commented 1 year ago

Thanks, @interkosmos for your suggestion, If I understand correctly, you are suggesting that we should perform all cURL related tasks in a single function or subroutine, which in our case would be client_get_response() within the http_client.f90 file. Additionally, you propose removing curl_ptr and header_list_ptr from the client_type and header_type derived types. Is my understanding correct? Please let me know if I have misunderstood your suggestion.

interkosmos commented 1 year ago

Yes, you are correct. But see it as a proposal we can discuss.

rajkumardongre commented 1 year ago

Alright, in the meantime, I will work on updating the API as per your recommendation in a separate branch.

rajkumardongre commented 1 year ago

Hello @interkosmos, I have implemented the necessary modifications in my latest pull request. I would appreciate it if you could review it.

milancurcic commented 1 year ago

Done by #21.