http-client icon indicating copy to clipboard operation
http-client copied to clipboard

Bug in request function when working with multipart/form-data

Open gha3mi opened this issue 2 years ago • 4 comments

I found a bug with the request function when working with multipart/form-data. The second and following pairs of form variables require an extra space at the beginning!

Here is the problem: GitHub Issue #28

Thanks, Ali

gha3mi avatar Sep 18 '23 18:09 gha3mi

Thanks, Ali. Do you know why is the extra space needed for the 2nd and following form pairs? I suppose the change is needed here:

https://github.com/fortran-lang/http-client/blob/d65b4b1eb96ec9a4ddbb98252335aa37ad24ace0/src/http/http_client.f90#L250-L262

I wonder if this has to do with how curl_easy_escape works.

milancurcic avatar Sep 18 '23 18:09 milancurcic

Thank you for your prompt response.

My test shows that the function prepare_form_encoded_str isn't being called. I think this function should be called within the client_get_response function.

gha3mi avatar Sep 18 '23 19:09 gha3mi

Hello Ali, as you are utilizing both the file and form concurrently, this code is currently operational. I will proceed to test this code: https://github.com/fortran-lang/http-client/blob/d65b4b1eb96ec9a4ddbb98252335aa37ad24ace0/src/http/http_client.f90#L440-L459

rajkumardongre avatar Sep 18 '23 19:09 rajkumardongre

Thank you for your response.

This solution may not be ideal, but it works:

! if both file and form are passed
if(allocated(request%form)) then
    part_ptr = curl_mime_addpart(mime_ptr)
    status = curl_mime_data(part_ptr, request%form(1)%value, CURL_ZERO_TERMINATED)
    status = curl_mime_name(part_ptr, request%form(1)%name)
    if (size(request%form) > 1) then
        do i=2,size(request%form)
            part_ptr = curl_mime_addpart(mime_ptr)
            status = curl_mime_data(part_ptr, request%form(i)%value, CURL_ZERO_TERMINATED)
            status = curl_mime_name(part_ptr, " "//request%form(i)%name)
        end do
    end if
end if

gha3mi avatar Sep 18 '23 20:09 gha3mi