added some more templates and functions

- added template for request body which takes either a string
	  build with form data or the json or text input given as it
	- added template for setting content type in header for
	  multipart/form-data file format
	- passing the request-body in setopt method to set the request
	  body
	- added functions to build formdata with or without a File
This commit is contained in:
Shruti Roy
2024-03-17 23:57:56 +05:30
parent 342fd78b5f
commit 53179f88e2

View File

@ -35,6 +35,13 @@ if (count(\$queryParams) > 0) {
""";
String kTemplateBody = """
\$request_body = <<<EOF
{{body}}
EOF;
""";
//specifying headers
String kTemplateHeaders = """
@ -43,7 +50,9 @@ curl_setopt(\$request, CURLOPT_HTTPHEADER, \$headers);
""";
//request template
String kTemplateFormHeaderContentType = '''
multipart/form-data; boundary={{boundary}}''';
String kTemplateRequest = """
curl_setopt(\$request, CURLOPT_RETURNTRANSFER, 1);
@ -51,6 +60,11 @@ curl_setopt(\$request, {{method}}, 1);
""";
//passing the request body
String kStringRequestBody = """
curl_setopt(\$request, CURLOPT_POSTFIELDS, \$request_body);
""";
//ending template
final String kStringRequestEnd = """
@ -86,11 +100,58 @@ class File
""";
//function to build formdata without 'file' type
String kBuildFormDataFunctionWithoutFilesString = """
function build_data(\$boundary, \$fields)
{
\$data = '';
\$eol = "\\r\\n";
\$delimiter = '-------------' . \$boundary;
foreach (\$fields as \$name => \$content) {
\$data .= "--" . \$delimiter . \$eol
. 'Content-Disposition: form-data; name="' . \$name . "\\"" . \$eol . \$eol
. \$content . \$eol;
}
\$data .= "--" . \$delimiter . "--" . \$eol;
return \$data;
}
""";
//function to build formdata with 'file' type
String kBuildFormDataFunctionWithFilesString = """
function build_data_files(\$boundary, \$fields, \$files)
{
\$data = '';
\$eol = "\\r\\n";
\$delimiter = '-------------' . \$boundary;
foreach (\$fields as \$name => \$content) {
\$data .= "--" . \$delimiter . \$eol
. 'Content-Disposition: form-data; name="' . \$name . "\\"" . \$eol . \$eol
. \$content . \$eol;
}
foreach (\$files as \$uploaded_file) {
if (\$uploaded_file instanceof File) {
\$data .= "--" . \$delimiter . \$eol
. 'Content-Disposition: form-data; name="' . \$uploaded_file->name . '"; filename="' . \$uploaded_file->filename . '"' . \$eol
. 'Content-Transfer-Encoding: binary' . \$eol;
\$data .= \$eol;
\$data .= \$uploaded_file->content . \$eol;
}
}
\$data .= "--" . \$delimiter . "--" . \$eol;
return \$data;
}
""";
//
String kMultiPartBodyWithFiles = """
\$request_body = build_data_files(\$boundary, \$fields, \$files);