Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
gha3mi opened this issue Sep 18, 2023 · 4 comments
Open

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

gha3mi opened this issue Sep 18, 2023 · 4 comments

Comments

@gha3mi
Copy link
Contributor

gha3mi commented Sep 18, 2023

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

@milancurcic
Copy link
Member

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:

if(allocated(request%form)) then
do i=1, size(request%form)
if(.not. allocated(form_encoded_str)) then
form_encoded_str = curl_easy_escape(curl_ptr, request%form(i)%name, &
len(request%form(i)%name)) // '=' // curl_easy_escape(curl_ptr, &
request%form(i)%value, len(request%form(i)%value))
else
form_encoded_str = form_encoded_str // '&' // &
curl_easy_escape(curl_ptr, request%form(i)%name, len(request%form(i)%name))&
// '=' // curl_easy_escape(curl_ptr, request%form(i)%value, len(request%form(i)%value))
end if
end do
end if

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

@gha3mi
Copy link
Contributor Author

gha3mi commented Sep 18, 2023

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.

@rajkumardongre
Copy link
Contributor

Hello Ali, as you are utilizing both the file and form concurrently, this code is currently operational. I will proceed to test this code:

else if (allocated(request%file)) then
mime_ptr = curl_mime_init(curl_ptr)
part_ptr = curl_mime_addpart(mime_ptr)
status = curl_mime_filedata(part_ptr, request%file%value)
status = curl_mime_name(part_ptr, request%file%name)
! if both file and form are passed
if(allocated(request%form)) then
do i=1, 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
status = curl_easy_setopt(curl_ptr, CURLOPT_MIMEPOST, mime_ptr)
! setting the Content-Type header to multipart/form-data, used for sending binary data
if (.not. pair_has_name(request%header, 'Content-Type')) then
call append_pair(request%header, 'Content-Type', 'multipart/form-data')
end if

@gha3mi
Copy link
Contributor Author

gha3mi commented Sep 18, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants