I have a simple perl cgi script that either displays a webpage to allow selection of a file, or, if a file was submitted, accepts the file and displays content to browser.
It works fine via browser. How do I simulate this using curl?
What I tried didnt work: curl -X POST -F [email protected] myserver.com/cgi-bin/script.cgi
Here is the perl script:
#!/usr/bin/perl
use CGI;
# Create CGI object
my $cgi = CGI->new;
# Print Content-Type header
print $cgi->header('text/html');
# Check if a file has been uploaded
if ($cgi->param('upload')) {
# Get the uploaded file handle
my $file_handle = $cgi->upload('file_input');
# Read and display the content of the file
if ($file_handle) {
print "<h1>File Content</h1>";
# Slurp the entire content of the file
my $file_content = do { local $/; <$file_handle> };
# Display the content
print "<pre>$file_content</pre>";
} else {
print "<p>Error reading the uploaded file.</p>";
}
} else {
# Display the file upload form
print <<HTML;
<!DOCTYPE html>
<html>
<head>
<title>File Upload Form</title>
</head>
<body>
<h1>File Upload Form</h1>
<form method="post" enctype="multipart/form-data">
<label for="file_input">Choose a file:</label>
<input type="file" name="file_input" id="file_input" required>
<br>
<input type="submit" name="upload" value="Upload">
</form>
</body>
</html>
HTML
}
You're missing the upload
parameter required by your program.
if ($cgi->param('upload')) {
Your form has a button which sends upload=Upload
.
<input type="submit" name="upload" value="Upload">
But you're not sending it with curl.