Skip to content

Commit

Permalink
httpRequest POST allows generic body #94
Browse files Browse the repository at this point in the history
  • Loading branch information
victordiaz committed Dec 24, 2020
1 parent 0f7d2ff commit 97ff27f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ buildscript {
}

project.ext {
versionCode = 134
versionName = "1.3.2"
versionCode = 135
versionName = "1.3.3pre"
compileSdkVersion = 29
targetSdkVersion = 29
minSdkVersion = 16
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ public class HTTPRequest {
private final String url;
private NativeObject headers;
private final NativeArray data;
private final NativeObject body;

private ReturnInterface callbackfn;

Expand All @@ -284,20 +285,23 @@ public HTTPRequest(NativeObject requestParams) {
url = (String) requestParams.get("url");
headers = (NativeObject) requestParams.get("headers");
data = (NativeArray) requestParams.get("data");
body = (NativeObject) requestParams.get("body");
}

public void request() {
final OkHttpClient client = new OkHttpClient();

Thread t = new Thread(() -> {
MultipartBody multipartBody = null;
RequestBody requestBody = null;
boolean dataExists = false;
boolean headersExists = false;

if (data != null) { if (data.size() > 0) dataExists = true; }
if (headers == null) {
headers = new NativeObject();
}

if (body != null) requestBody = RequestBody.create(MediaType.parse((String) body.get("type")), (String) body.get("data"));

if (dataExists) {
MultipartBody.Builder formBody = new MultipartBody.Builder();
formBody.setType(MultipartBody.FORM);
Expand All @@ -313,21 +317,31 @@ public void request() {
if (type.equals("file")) {
String mediaType = (String) o.get("mediaType");
File f = new File(getAppRunner().getProject().getFullPathForFile(content));
// MLog.d("nn1", f.getAbsolutePath() + " " + content + " " + name + " " + mediaType);

try {
if (!f.exists()) {
throw new Exception("File does not exist");
}
} catch (Exception e) {
getAppRunner().pConsole.p_error(AppRunnerInterpreter.RESULT_ERROR, e.getMessage());
e.printStackTrace();
return;
}

formBody.addFormDataPart(name, content, RequestBody.create(MediaType.parse(mediaType), f));
} else {
formBody.addFormDataPart(name, content);
}
}
multipartBody = formBody.build();
requestBody = formBody.build();
}

// Map<String, String> header = new HashMap<String, String>();

Request request;
try {
Headers buildHeaders = Headers.of(headers);
request = new Request.Builder().headers(buildHeaders).url(url).method(method, multipartBody).build();
request = new Request.Builder().headers(buildHeaders).url(url).method(method, requestBody).build();
} catch (Exception e) {
getAppRunner().pConsole.p_error(AppRunnerInterpreter.RESULT_ERROR, e.toString());
return;
Expand Down

0 comments on commit 97ff27f

Please sign in to comment.