@@ -9,6 +9,7 @@ import mars.Request;
99import mars.RequestParams;
1010import mars.ServerException;
1111import mars.StatusCode;
12+ import mars.UrlHelper;
1213
1314class AsyncHttpClient : HttpClient {
1415
@@ -50,39 +51,21 @@ class AsyncHttpClient : HttpClient {
5051 import std.stdio ;
5152 import std.conv ;
5253
53- string url = getCorrectUrl(request.getUrl);
54- writeln(" url: " , url);
55-
5654 int statusCode = StatusCode.BAD_REQUEST ;
5755 string [string ] responseHeaders;
5856 ubyte [] responseBody;
5957
6058 mHttp.method = httpMethod;
61- if (httpMethod == HTTP .Method.post || httpMethod == HTTP .Method.del || httpMethod == HTTP .Method.patch || httpMethod == HTTP .Method.put) {
62- mHttp.url = url;
59+ mHttp.url = UrlHelper.createUrl(mOptions.baseUrl, request.getUrl, request.getParams);
60+
61+ if (httpMethod != HTTP .Method.get ) {
6362 string contentType = " Content-Type" in mOptions.headers ? mOptions.headers[" Content-Type" ] : null ;
6463 if (contentType is null ) {
6564 contentType = " Content-Type" in request.getHeaders ? request.getHeaders[" Content-Type" ] : " application/json" ;
6665 }
67- mHttp.setPostData(request.getParams.toJson, contentType);
68- } else if (httpMethod == HTTP .Method.get ) {
69- string [string ] paramsToSend = request.getParams.getParams;
70-
71- if (paramsToSend ! is null && paramsToSend.length > 0 ) {
72- import std.string ;
73- import std.algorithm.searching ;
74- import std.algorithm.iteration ;
75-
76- string urlParams = paramsToSend.keys .map! (k => k ~ " =" ~ paramsToSend[k]).join(" &" );
77- if (canFind(url, " ?" )) {
78- mHttp.url = url ~ " &" ~ urlParams;
79- } else {
80- mHttp.url = url ~ " ?" ~ urlParams;
81- }
82- } else {
83- mHttp.url = url;
84- }
66+ mHttp.setPostData(request.getData, contentType);
8567 }
68+
8669 if (request.getHeaders != null && request.getHeaders.length > 0 ) {
8770 foreach (name, value; request.getHeaders) {
8871 mHttp.addRequestHeader(name, value);
@@ -128,18 +111,4 @@ class AsyncHttpClient : HttpClient {
128111 }
129112 }
130113 }
131-
132- private string getCorrectUrl (string url) {
133- import std.exception ;
134-
135- enforce(mOptions.baseUrl ! is null , new Exception (" Cannot get URL, please, set base URL!" ));
136-
137- import std.algorithm.searching ;
138-
139- if (url.startsWith(" http" )) {
140- return url;
141- }
142-
143- return mOptions.baseUrl ~ url;
144- }
145114}
0 commit comments