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

Support multipart/form-data #1115

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

hatamiarash7
Copy link

@hatamiarash7 hatamiarash7 commented Aug 27, 2023

Description

After this PR, we can send POST requests in multipart/form-data format. This allows users to test the upload process quickly.

http_post_body_multipart:
    prober: http
    timeout: 5s
    http:
      method: POST
      body_multipart:
        - type: "file"
          key: "file"
          value: "/files/body.txt"
        - type: "text"
          key: "name"
          value: "arash"

We can define text or file fields here.


Changes

  • Support multipart for HTTP probe
  • Update example file
  • Update configuration document
  • Add a new test for multipart form request

Signed-off-by: Arash Hatami <[email protected]>
Signed-off-by: Arash Hatami <[email protected]>
Signed-off-by: Arash Hatami <[email protected]>
@hatamiarash7
Copy link
Author

any update?

@github-actions github-actions bot added the stale label Sep 19, 2024
@hatamiarash7 hatamiarash7 force-pushed the http-multipart branch 2 times, most recently from 7de228d to e76f9c3 Compare December 27, 2024 19:33
@github-actions github-actions bot removed the stale label Dec 27, 2024
@hatamiarash7
Copy link
Author

Hello

Do we have an update for this PR?

The latest changes have been synced and new tests have been added for multipart.

Unfortunately, the IPv6 tests did not pass, and #1342 will solve the problem. Otherwise, a separate update is needed.

CONFIGURATION.md Show resolved Hide resolved
@@ -424,6 +426,60 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr
body = body_file
}

// If a multipart form is configured, add it to the request.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this implementation and configuration feels a little awkward, can we make it simple and implement this with just one bool flag called multipart?

If the flag is set, you can just make the check multi-part. for text and data, you can use the existing Body and BodyFile config values?

this would make it easy to configure and simplify this implementation as well?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a multipart request, submitting multiple files (not just one file or text) should be possible. This is not possible using Body and BodyFile.

Copy link
Member

@electron0zero electron0zero Jan 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I read up some literature more on the multipart, and I still don't understand this code.

type Multipart struct {
	Type  string `yaml:"type,omitempty"`
	Key   string `yaml:"key,omitempty"`
	Value string `yaml:"value,omitempty"`
} 

the part I am having hard time understanding is the why behind Multipart struct and the Type, and the Key and Value in it.

do you need multiple text fields? if yes, can you share more info on it?

I think it should be possible to have it something like. MultipartFiles []string, and pass multiple filenames for the multi-part check, and append them to the Body and use the Body or BodyText for the body text.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imagine a scenario where two files and two texts are to be sent. Consider the following curl example:

curl --location 'https://domain.com' \
--form 'text-key-1="text-value-1"' \
--form 'text-key-2="text-value-2"' \
--form 'file-key-1=@"/files/file-1.jpeg"' \
--form 'file-key-2=@"/files/file-2.jpeg"'

If we want to implement something like this, we need a separate structure to set the Type (text/file), Key, and Value.

http_post_body_multipart:
  prober: http
  timeout: 5s
  http:
    method: POST
    body_multipart:
      - type: "text-key-1"
        key: "name"
        value: "text-value-1"
      - type: "text-key-2"
        key: "name"
        value: "text-value-2"
      - type: "file"
        key: "file-key-1"
        value: "/files/file-1.jpeg"
      - type: "file"
        key: "file-key-2"
        value: "/files/file-2.jpeg"

@@ -1554,3 +1555,108 @@ func TestBody(t *testing.T) {
}
}
}

// Test that the HTTP probe module can handle a multipart form request
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this test can also be simplified after the implementation is updated.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As explained in the previous comment, we need to be able to POST different files/text. For this purpose, the bodyMultipart variable has been defined and used in this test.

@electron0zero
Copy link
Member

for CI failure: #1342 is merged, please rebase.

@hatamiarash7
Copy link
Author

for CI failure: #1342 is merged, please rebase.

Rebased.

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

Successfully merging this pull request may close these issues.

2 participants