mirror of
https://github.com/foss42/apidash.git
synced 2025-08-06 05:32:26 +08:00
fixes
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
import 'package:jinja/jinja.dart' as jj;
|
||||
import 'package:apidash/utils/utils.dart'
|
||||
show requestModelToHARJsonRequest, stripUrlParams;
|
||||
show stripUrlParams;
|
||||
import 'package:apidash/models/models.dart' show RequestModel;
|
||||
import 'package:apidash/consts.dart';
|
||||
|
||||
@ -16,7 +16,7 @@ use GuzzleHttp\\Psr7\\Request;
|
||||
""";
|
||||
|
||||
String kMultiPartBodyTemplate = """
|
||||
\$multipart = new MultipartStream([
|
||||
\$body = new MultipartStream([
|
||||
{{fields_list}}
|
||||
]);
|
||||
|
||||
@ -41,7 +41,9 @@ use GuzzleHttp\\Psr7\\Request;
|
||||
""";
|
||||
|
||||
String kTemplateBody = """
|
||||
\$body = {{body}};
|
||||
\$body = <<<END
|
||||
{{body}}
|
||||
END;
|
||||
|
||||
|
||||
""";
|
||||
@ -80,84 +82,54 @@ echo $res->getBody();
|
||||
result += renderedMultiPartBody;
|
||||
}
|
||||
|
||||
var harJson =
|
||||
requestModelToHARJsonRequest(requestModel, useEnabled: true);
|
||||
|
||||
var params = harJson["queryString"];
|
||||
var params = requestModel.enabledParamsMap;
|
||||
if (params.isNotEmpty) {
|
||||
var templateParams = jj.Template(kTemplateParams);
|
||||
var m = {};
|
||||
for (var i in params) {
|
||||
m[i["name"]] = i["value"];
|
||||
}
|
||||
var jsonString = '';
|
||||
m.forEach((key, value) {
|
||||
jsonString += "'$key' => '$value',\n";
|
||||
List<String> paramList = [];
|
||||
params.forEach((key, value) {
|
||||
paramList.add("'$key' => '$value'");
|
||||
});
|
||||
jsonString = jsonString.substring(0, jsonString.length - 2);
|
||||
result += templateParams.render({
|
||||
"params": jsonString,
|
||||
"params": paramList.join(",\n"),
|
||||
});
|
||||
}
|
||||
|
||||
var headers = harJson["headers"];
|
||||
if (headers.isNotEmpty || requestModel.hasFormData) {
|
||||
var headers = requestModel.enabledHeadersMap;
|
||||
List<String> headerList = [];
|
||||
if (headers.isNotEmpty || requestModel.hasBody) {
|
||||
var templateHeader = jj.Template(kTemplateHeader);
|
||||
var m = {};
|
||||
for (var i in headers) {
|
||||
m[i["name"]] = i["value"];
|
||||
}
|
||||
var headersString = '';
|
||||
var contentTypeAdded = false;
|
||||
|
||||
m.forEach((key, value) {
|
||||
if (key == 'Content-Type' && value.contains('multipart/form-data')) {
|
||||
contentTypeAdded = false;
|
||||
} else {
|
||||
headersString += "'$key' => '$value',\n";
|
||||
}
|
||||
headers.forEach((key, value) {
|
||||
headerList.add("'$key' => '$value'");
|
||||
});
|
||||
|
||||
if (requestModel.hasFormData && !contentTypeAdded) {
|
||||
headersString +=
|
||||
"'Content-Type' => 'multipart/form-data; boundary=' . \$multipart->getBoundary(), \n";
|
||||
if(!requestModel.hasContentTypeHeader && requestModel.hasBody){
|
||||
if(requestModel.hasJsonData || requestModel.hasTextData){
|
||||
headerList.add("'$kHeaderContentType' => '${requestModel.requestBodyContentType.header}'");
|
||||
}
|
||||
if(requestModel.hasFormData){
|
||||
headerList.add("'$kHeaderContentType' => '${requestModel.requestBodyContentType.header}; boundary=' . \$body->getBoundary()");
|
||||
}
|
||||
}
|
||||
headersString = headersString.substring(0, headersString.length - 2);
|
||||
result += templateHeader.render({
|
||||
"headers": headersString,
|
||||
"headers": headerList.join(",\n"),
|
||||
});
|
||||
}
|
||||
|
||||
var templateBody = jj.Template(kTemplateBody);
|
||||
|
||||
if (harJson["postData"]?["text"] != null) {
|
||||
if (requestModel.hasJsonData || requestModel.hasTextData) {
|
||||
result += templateBody
|
||||
.render({"body": kEncoder.convert(harJson["postData"]["text"])});
|
||||
}
|
||||
|
||||
String getRequestBody(Map harJson) {
|
||||
if (harJson.containsKey("postData")) {
|
||||
var postData = harJson["postData"];
|
||||
if (postData.containsKey("mimeType")) {
|
||||
var mimeType = postData["mimeType"];
|
||||
if (mimeType == "text/plain" || mimeType == "application/json") {
|
||||
return " \$body";
|
||||
} else if (mimeType.contains("multipart/form-data")) {
|
||||
return " \$multipart";
|
||||
}
|
||||
}
|
||||
}
|
||||
return "";
|
||||
.render({"body": requestModel.requestBody});
|
||||
}
|
||||
|
||||
var templateRequest = jj.Template(kStringRequest);
|
||||
result += templateRequest.render({
|
||||
"url": stripUrlParams(requestModel.url),
|
||||
"method": harJson["method"].toLowerCase(),
|
||||
"method": requestModel.method.name.toLowerCase(),
|
||||
"queryParams":
|
||||
harJson["queryString"].isNotEmpty ? ". \$queryParamsStr" : "",
|
||||
"headers": harJson["headers"].isNotEmpty ? ", \$headers," : "",
|
||||
"body": getRequestBody(harJson),
|
||||
params.isNotEmpty ? ". \$queryParamsStr" : "",
|
||||
"headers": headerList.isNotEmpty ? ", \$headers" : "",
|
||||
"body": requestModel.hasBody? ", \$body" : "",
|
||||
});
|
||||
|
||||
return result;
|
||||
|
@ -124,7 +124,7 @@ $headers = [
|
||||
|
||||
$client = new Client();
|
||||
|
||||
$request = new Request('get', 'https://api.github.com/repos/foss42/apidash' , $headers, );
|
||||
$request = new Request('get', 'https://api.github.com/repos/foss42/apidash', $headers);
|
||||
$res = $client->sendAsync($request)->wait();
|
||||
|
||||
echo $res->getStatusCode() . "\n";
|
||||
@ -154,7 +154,7 @@ $headers = [
|
||||
|
||||
$client = new Client();
|
||||
|
||||
$request = new Request('get', 'https://api.github.com/repos/foss42/apidash'. $queryParamsStr , $headers, );
|
||||
$request = new Request('get', 'https://api.github.com/repos/foss42/apidash'. $queryParamsStr, $headers);
|
||||
$res = $client->sendAsync($request)->wait();
|
||||
|
||||
echo $res->getStatusCode() . "\n";
|
||||
@ -205,7 +205,7 @@ $headers = [
|
||||
|
||||
$client = new Client();
|
||||
|
||||
$request = new Request('get', 'https://api.github.com/repos/foss42/apidash'. $queryParamsStr , $headers, );
|
||||
$request = new Request('get', 'https://api.github.com/repos/foss42/apidash'. $queryParamsStr, $headers);
|
||||
$res = $client->sendAsync($request)->wait();
|
||||
|
||||
echo $res->getStatusCode() . "\n";
|
||||
@ -257,7 +257,7 @@ $headers = [
|
||||
|
||||
$client = new Client();
|
||||
|
||||
$request = new Request('get', 'https://api.apidash.dev/humanize/social' , $headers, );
|
||||
$request = new Request('get', 'https://api.apidash.dev/humanize/social', $headers);
|
||||
$res = $client->sendAsync($request)->wait();
|
||||
|
||||
echo $res->getStatusCode() . "\n";
|
||||
@ -292,7 +292,7 @@ $headers = [
|
||||
|
||||
$client = new Client();
|
||||
|
||||
$request = new Request('get', 'https://api.apidash.dev/humanize/social'. $queryParamsStr , $headers, );
|
||||
$request = new Request('get', 'https://api.apidash.dev/humanize/social'. $queryParamsStr, $headers);
|
||||
$res = $client->sendAsync($request)->wait();
|
||||
|
||||
echo $res->getStatusCode() . "\n";
|
||||
@ -385,7 +385,11 @@ $headers = [
|
||||
'Content-Type' => 'text/plain'
|
||||
];
|
||||
|
||||
$body = "{\n\"text\": \"I LOVE Flutter\"\n}";
|
||||
$body = <<<END
|
||||
{
|
||||
"text": "I LOVE Flutter"
|
||||
}
|
||||
END;
|
||||
|
||||
$client = new Client();
|
||||
|
||||
@ -413,7 +417,16 @@ $headers = [
|
||||
'Content-Type' => 'application/json'
|
||||
];
|
||||
|
||||
$body = "{\n\"text\": \"I LOVE Flutter\",\n\"flag\": null,\n\"male\": true,\n\"female\": false,\n\"no\": 1.2,\n\"arr\": [\"null\", \"true\", \"false\", null]\n}";
|
||||
$body = <<<END
|
||||
{
|
||||
"text": "I LOVE Flutter",
|
||||
"flag": null,
|
||||
"male": true,
|
||||
"female": false,
|
||||
"no": 1.2,
|
||||
"arr": ["null", "true", "false", null]
|
||||
}
|
||||
END;
|
||||
|
||||
$client = new Client();
|
||||
|
||||
@ -438,11 +451,15 @@ use GuzzleHttp\Psr7\Request;
|
||||
|
||||
|
||||
$headers = [
|
||||
'Content-Type' => 'application/json',
|
||||
'User-Agent' => 'Test Agent'
|
||||
'User-Agent' => 'Test Agent',
|
||||
'Content-Type' => 'application/json'
|
||||
];
|
||||
|
||||
$body = "{\n\"text\": \"I LOVE Flutter\"\n}";
|
||||
$body = <<<END
|
||||
{
|
||||
"text": "I LOVE Flutter"
|
||||
}
|
||||
END;
|
||||
|
||||
$client = new Client();
|
||||
|
||||
@ -466,7 +483,7 @@ use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use GuzzleHttp\Psr7\MultipartStream;
|
||||
|
||||
$multipart = new MultipartStream([
|
||||
$body = new MultipartStream([
|
||||
[
|
||||
'name' => 'text',
|
||||
'contents' => 'API'
|
||||
@ -483,12 +500,12 @@ $multipart = new MultipartStream([
|
||||
]);
|
||||
|
||||
$headers = [
|
||||
'Content-Type' => 'multipart/form-data; boundary=' . $multipart->getBoundary(),
|
||||
'Content-Type' => 'multipart/form-data; boundary=' . $body->getBoundary()
|
||||
];
|
||||
|
||||
$client = new Client();
|
||||
|
||||
$request = new Request('post', 'https://api.apidash.dev/io/form' , $headers, $multipart);
|
||||
$request = new Request('post', 'https://api.apidash.dev/io/form', $headers, $body);
|
||||
$res = $client->sendAsync($request)->wait();
|
||||
|
||||
echo $res->getStatusCode() . "\n";
|
||||
@ -508,7 +525,7 @@ use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use GuzzleHttp\Psr7\MultipartStream;
|
||||
|
||||
$multipart = new MultipartStream([
|
||||
$body = new MultipartStream([
|
||||
[
|
||||
'name' => 'text',
|
||||
'contents' => 'API'
|
||||
@ -526,12 +543,12 @@ $multipart = new MultipartStream([
|
||||
|
||||
$headers = [
|
||||
'User-Agent' => 'Test Agent',
|
||||
'Content-Type' => 'multipart/form-data; boundary=' . $multipart->getBoundary(),
|
||||
'Content-Type' => 'multipart/form-data; boundary=' . $body->getBoundary()
|
||||
];
|
||||
|
||||
$client = new Client();
|
||||
|
||||
$request = new Request('post', 'https://api.apidash.dev/io/form' , $headers, $multipart);
|
||||
$request = new Request('post', 'https://api.apidash.dev/io/form', $headers, $body);
|
||||
$res = $client->sendAsync($request)->wait();
|
||||
|
||||
echo $res->getStatusCode() . "\n";
|
||||
@ -551,7 +568,7 @@ use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use GuzzleHttp\Psr7\MultipartStream;
|
||||
|
||||
$multipart = new MultipartStream([
|
||||
$body = new MultipartStream([
|
||||
[
|
||||
'name' => 'token',
|
||||
'contents' => 'xyz'
|
||||
@ -564,12 +581,12 @@ $multipart = new MultipartStream([
|
||||
]);
|
||||
|
||||
$headers = [
|
||||
'Content-Type' => 'multipart/form-data; boundary=' . $multipart->getBoundary(),
|
||||
'Content-Type' => 'multipart/form-data; boundary=' . $body->getBoundary()
|
||||
];
|
||||
|
||||
$client = new Client();
|
||||
|
||||
$request = new Request('post', 'https://api.apidash.dev/io/img' , $headers, $multipart);
|
||||
$request = new Request('post', 'https://api.apidash.dev/io/img', $headers, $body);
|
||||
$res = $client->sendAsync($request)->wait();
|
||||
|
||||
echo $res->getStatusCode() . "\n";
|
||||
@ -589,7 +606,7 @@ use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use GuzzleHttp\Psr7\MultipartStream;
|
||||
|
||||
$multipart = new MultipartStream([
|
||||
$body = new MultipartStream([
|
||||
[
|
||||
'name' => 'token',
|
||||
'contents' => 'xyz'
|
||||
@ -602,12 +619,12 @@ $multipart = new MultipartStream([
|
||||
]);
|
||||
|
||||
$headers = [
|
||||
'Content-Type' => 'multipart/form-data; boundary=' . $multipart->getBoundary(),
|
||||
'Content-Type' => 'multipart/form-data; boundary=' . $body->getBoundary()
|
||||
];
|
||||
|
||||
$client = new Client();
|
||||
|
||||
$request = new Request('post', 'https://api.apidash.dev/io/img' , $headers, $multipart);
|
||||
$request = new Request('post', 'https://api.apidash.dev/io/img', $headers, $body);
|
||||
$res = $client->sendAsync($request)->wait();
|
||||
|
||||
echo $res->getStatusCode() . "\n";
|
||||
@ -627,7 +644,7 @@ use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use GuzzleHttp\Psr7\MultipartStream;
|
||||
|
||||
$multipart = new MultipartStream([
|
||||
$body = new MultipartStream([
|
||||
[
|
||||
'name' => 'text',
|
||||
'contents' => 'API'
|
||||
@ -650,12 +667,12 @@ $queryParams = [
|
||||
$queryParamsStr = '?' . http_build_query($queryParams);
|
||||
|
||||
$headers = [
|
||||
'Content-Type' => 'multipart/form-data; boundary=' . $multipart->getBoundary(),
|
||||
'Content-Type' => 'multipart/form-data; boundary=' . $body->getBoundary()
|
||||
];
|
||||
|
||||
$client = new Client();
|
||||
|
||||
$request = new Request('post', 'https://api.apidash.dev/io/form'. $queryParamsStr , $headers, $multipart);
|
||||
$request = new Request('post', 'https://api.apidash.dev/io/form'. $queryParamsStr, $headers, $body);
|
||||
$res = $client->sendAsync($request)->wait();
|
||||
|
||||
echo $res->getStatusCode() . "\n";
|
||||
@ -675,7 +692,7 @@ use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use GuzzleHttp\Psr7\MultipartStream;
|
||||
|
||||
$multipart = new MultipartStream([
|
||||
$body = new MultipartStream([
|
||||
[
|
||||
'name' => 'token',
|
||||
'contents' => 'xyz'
|
||||
@ -696,12 +713,12 @@ $queryParamsStr = '?' . http_build_query($queryParams);
|
||||
$headers = [
|
||||
'User-Agent' => 'Test Agent',
|
||||
'Keep-Alive' => 'true',
|
||||
'Content-Type' => 'multipart/form-data; boundary=' . $multipart->getBoundary(),
|
||||
'Content-Type' => 'multipart/form-data; boundary=' . $body->getBoundary()
|
||||
];
|
||||
|
||||
$client = new Client();
|
||||
|
||||
$request = new Request('post', 'https://api.apidash.dev/io/img'. $queryParamsStr , $headers, $multipart);
|
||||
$request = new Request('post', 'https://api.apidash.dev/io/img'. $queryParamsStr, $headers, $body);
|
||||
$res = $client->sendAsync($request)->wait();
|
||||
|
||||
echo $res->getStatusCode() . "\n";
|
||||
@ -727,7 +744,12 @@ $headers = [
|
||||
'Content-Type' => 'application/json'
|
||||
];
|
||||
|
||||
$body = "{\n\"name\": \"morpheus\",\n\"job\": \"zion resident\"\n}";
|
||||
$body = <<<END
|
||||
{
|
||||
"name": "morpheus",
|
||||
"job": "zion resident"
|
||||
}
|
||||
END;
|
||||
|
||||
$client = new Client();
|
||||
|
||||
@ -756,7 +778,12 @@ $headers = [
|
||||
'Content-Type' => 'application/json'
|
||||
];
|
||||
|
||||
$body = "{\n\"name\": \"marfeus\",\n\"job\": \"accountant\"\n}";
|
||||
$body = <<<END
|
||||
{
|
||||
"name": "marfeus",
|
||||
"job": "accountant"
|
||||
}
|
||||
END;
|
||||
|
||||
$client = new Client();
|
||||
|
||||
@ -808,7 +835,12 @@ $headers = [
|
||||
'Content-Type' => 'application/json'
|
||||
];
|
||||
|
||||
$body = "{\n\"name\": \"marfeus\",\n\"job\": \"accountant\"\n}";
|
||||
$body = <<<END
|
||||
{
|
||||
"name": "marfeus",
|
||||
"job": "accountant"
|
||||
}
|
||||
END;
|
||||
|
||||
$client = new Client();
|
||||
|
||||
|
Reference in New Issue
Block a user