diff --git a/plugin.xml b/plugin.xml
index 609f08d..381bcf0 100644
--- a/plugin.xml
+++ b/plugin.xml
@@ -70,14 +70,16 @@
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
diff --git a/src/android/com/synconset/CordovaHTTP/CordovaHttpDelete.java b/src/android/com/synconset/CordovaHTTP/CordovaHttpDelete.java
new file mode 100644
index 0000000..604e6a5
--- /dev/null
+++ b/src/android/com/synconset/CordovaHTTP/CordovaHttpDelete.java
@@ -0,0 +1,58 @@
+/**
+ * A HTTP plugin for Cordova / Phonegap
+ */
+package com.synconset.cordovahttp;
+
+import java.net.UnknownHostException;
+
+import javax.net.ssl.SSLHandshakeException;
+
+import org.apache.cordova.CallbackContext;
+
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import com.github.kevinsawicki.http.HttpRequest;
+import com.github.kevinsawicki.http.HttpRequest.HttpRequestException;
+
+class CordovaHttpDelete extends CordovaHttp implements Runnable {
+ public CordovaHttpDelete(String urlString, JSONObject data, JSONObject headers, CallbackContext callbackContext) {
+ super(urlString, data, headers, callbackContext);
+ }
+
+ @Override
+ public void run() {
+ try {
+ HttpRequest request = HttpRequest.delete(this.getUrlString(), this.getParamsMap(), false);
+
+ this.setupSecurity(request);
+ request.acceptCharset(CHARSET);
+ request.headers(this.getHeadersMap());
+
+ int code = request.code();
+ String body = request.body(CHARSET);
+ JSONObject response = new JSONObject();
+
+ this.addResponseHeaders(request, response);
+ response.put("status", code);
+
+ if (code >= 200 && code < 300) {
+ response.put("data", body);
+ this.getCallbackContext().success(response);
+ } else {
+ response.put("error", body);
+ this.getCallbackContext().error(response);
+ }
+ } catch (JSONException e) {
+ this.respondWithError("There was an error generating the response");
+ } catch (HttpRequestException e) {
+ if (e.getCause() instanceof UnknownHostException) {
+ this.respondWithError(0, "The host could not be resolved");
+ } else if (e.getCause() instanceof SSLHandshakeException) {
+ this.respondWithError("SSL handshake failed");
+ } else {
+ this.respondWithError("There was an error with the request");
+ }
+ }
+ }
+}
diff --git a/src/android/com/synconset/CordovaHTTP/CordovaHttpPut.java b/src/android/com/synconset/CordovaHTTP/CordovaHttpPut.java
new file mode 100644
index 0000000..ac533d2
--- /dev/null
+++ b/src/android/com/synconset/CordovaHTTP/CordovaHttpPut.java
@@ -0,0 +1,64 @@
+/**
+ * A HTTP plugin for Cordova / Phonegap
+ */
+package com.synconset.cordovahttp;
+
+import java.net.UnknownHostException;
+
+import org.apache.cordova.CallbackContext;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import javax.net.ssl.SSLHandshakeException;
+
+import com.github.kevinsawicki.http.HttpRequest;
+import com.github.kevinsawicki.http.HttpRequest.HttpRequestException;
+
+class CordovaHttpPut extends CordovaHttp implements Runnable {
+ public CordovaHttpPut(String urlString, JSONObject data, String serializerName, JSONObject headers, CallbackContext callbackContext) {
+ super(urlString, data, serializerName, headers, callbackContext);
+ }
+
+ @Override
+ public void run() {
+ try {
+ HttpRequest request = HttpRequest.put(this.getUrlString());
+
+ this.setupSecurity(request);
+ request.acceptCharset(CHARSET);
+ request.headers(this.getHeadersMap());
+
+ if (new String("json").equals(this.getSerializerName())) {
+ request.contentType(request.CONTENT_TYPE_JSON, request.CHARSET_UTF8);
+ request.send(this.getParamsObject().toString());
+ } else {
+ request.form(this.getParamsMap());
+ }
+
+ int code = request.code();
+ String body = request.body(CHARSET);
+ JSONObject response = new JSONObject();
+
+ this.addResponseHeaders(request, response);
+ response.put("status", code);
+
+ if (code >= 200 && code < 300) {
+ response.put("data", body);
+ this.getCallbackContext().success(response);
+ } else {
+ response.put("error", body);
+ this.getCallbackContext().error(response);
+ }
+ } catch (JSONException e) {
+ this.respondWithError("There was an error generating the response");
+ } catch (HttpRequestException e) {
+ if (e.getCause() instanceof UnknownHostException) {
+ this.respondWithError(0, "The host could not be resolved");
+ } else if (e.getCause() instanceof SSLHandshakeException) {
+ this.respondWithError("SSL handshake failed");
+ } else {
+ this.respondWithError("There was an error with the request");
+ }
+ }
+ }
+}
diff --git a/src/android/com/synconset/cordovahttp/CordovaHttpPlugin.java b/src/android/com/synconset/cordovahttp/CordovaHttpPlugin.java
index c39693e..8e2c99a 100644
--- a/src/android/com/synconset/cordovahttp/CordovaHttpPlugin.java
+++ b/src/android/com/synconset/cordovahttp/CordovaHttpPlugin.java
@@ -34,21 +34,7 @@ public class CordovaHttpPlugin extends CordovaPlugin {
@Override
public boolean execute(String action, final JSONArray args, final CallbackContext callbackContext) throws JSONException {
- if (action.equals("get")) {
- String urlString = args.getString(0);
- JSONObject params = args.getJSONObject(1);
- JSONObject headers = args.getJSONObject(2);
- CordovaHttpGet get = new CordovaHttpGet(urlString, params, headers, callbackContext);
-
- cordova.getThreadPool().execute(get);
- } else if (action.equals("head")) {
- String urlString = args.getString(0);
- JSONObject params = args.getJSONObject(1);
- JSONObject headers = args.getJSONObject(2);
- CordovaHttpHead head = new CordovaHttpHead(urlString, params, headers, callbackContext);
-
- cordova.getThreadPool().execute(head);
- } else if (action.equals("post")) {
+ if (action.equals("post")) {
String urlString = args.getString(0);
JSONObject params = args.getJSONObject(1);
String serializerName = args.getString(2);
@@ -56,6 +42,35 @@ public class CordovaHttpPlugin extends CordovaPlugin {
CordovaHttpPost post = new CordovaHttpPost(urlString, params, serializerName, headers, callbackContext);
cordova.getThreadPool().execute(post);
+ } else if (action.equals("get")) {
+ String urlString = args.getString(0);
+ JSONObject params = args.getJSONObject(1);
+ JSONObject headers = args.getJSONObject(2);
+ CordovaHttpGet get = new CordovaHttpGet(urlString, params, headers, callbackContext);
+
+ cordova.getThreadPool().execute(get);
+ } else if (action.equals("put")) {
+ String urlString = args.getString(0);
+ JSONObject params = args.getJSONObject(1);
+ String serializerName = args.getString(2);
+ JSONObject headers = args.getJSONObject(3);
+ CordovaHttpPut put = new CordovaHttpPut(urlString, params, serializerName, headers, callbackContext);
+
+ cordova.getThreadPool().execute(put);
+ } else if (action.equals("delete")) {
+ String urlString = args.getString(0);
+ JSONObject params = args.getJSONObject(1);
+ JSONObject headers = args.getJSONObject(2);
+ CordovaHttpDelete delete = new CordovaHttpDelete(urlString, params, headers, callbackContext);
+
+ cordova.getThreadPool().execute(delete);
+ } else if (action.equals("head")) {
+ String urlString = args.getString(0);
+ JSONObject params = args.getJSONObject(1);
+ JSONObject headers = args.getJSONObject(2);
+ CordovaHttpHead head = new CordovaHttpHead(urlString, params, headers, callbackContext);
+
+ cordova.getThreadPool().execute(head);
} else if (action.equals("enableSSLPinning")) {
try {
boolean enable = args.getBoolean(0);
diff --git a/src/ios/CordovaHttpPlugin.h b/src/ios/CordovaHttpPlugin.h
index 917349a..24cc395 100644
--- a/src/ios/CordovaHttpPlugin.h
+++ b/src/ios/CordovaHttpPlugin.h
@@ -9,7 +9,9 @@
- (void)validateDomainName:(CDVInvokedUrlCommand*)command;
- (void)post:(CDVInvokedUrlCommand*)command;
- (void)get:(CDVInvokedUrlCommand*)command;
+- (void)put:(CDVInvokedUrlCommand*)command;
+- (void)delete:(CDVInvokedUrlCommand*)command;
- (void)uploadFile:(CDVInvokedUrlCommand*)command;
- (void)downloadFile:(CDVInvokedUrlCommand*)command;
-@end
\ No newline at end of file
+@end
diff --git a/src/ios/CordovaHttpPlugin.m b/src/ios/CordovaHttpPlugin.m
index 3214524..460afd0 100644
--- a/src/ios/CordovaHttpPlugin.m
+++ b/src/ios/CordovaHttpPlugin.m
@@ -129,6 +129,62 @@
}];
}
+- (void)put:(CDVInvokedUrlCommand*)command {
+ AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
+ manager.securityPolicy = securityPolicy;
+ NSString *url = [command.arguments objectAtIndex:0];
+ NSDictionary *parameters = [command.arguments objectAtIndex:1];
+ NSString *serializerName = [command.arguments objectAtIndex:2];
+ NSDictionary *headers = [command.arguments objectAtIndex:3];
+ [self setRequestSerializer: serializerName forManager: manager];
+ [self setRequestHeaders: headers forManager: manager];
+
+ CordovaHttpPlugin* __weak weakSelf = self;
+ manager.responseSerializer = [TextResponseSerializer serializer];
+ [manager PUT:url parameters:parameters success:^(NSURLSessionTask *task, id responseObject) {
+ NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
+ [self setResults: dictionary withTask: task];
+ [dictionary setObject:responseObject forKey:@"data"];
+ CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary];
+ [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
+ } failure:^(NSURLSessionTask *task, NSError *error) {
+ NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
+ [self setResults: dictionary withTask: task];
+ NSString* errResponse = [[NSString alloc] initWithData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] encoding:NSUTF8StringEncoding];
+ [dictionary setObject:errResponse forKey:@"error"];
+ CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
+ [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
+ }];
+}
+
+- (void)delete:(CDVInvokedUrlCommand*)command {
+ AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
+ manager.securityPolicy = securityPolicy;
+ NSString *url = [command.arguments objectAtIndex:0];
+ NSDictionary *parameters = [command.arguments objectAtIndex:1];
+ NSDictionary *headers = [command.arguments objectAtIndex:2];
+ [self setRequestSerializer: @"default" forManager: manager];
+ [self setRequestHeaders: headers forManager: manager];
+
+ CordovaHttpPlugin* __weak weakSelf = self;
+
+ manager.responseSerializer = [TextResponseSerializer serializer];
+ [manager DELETE:url parameters:parameters success:^(NSURLSessionTask *task, id responseObject) {
+ NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
+ [self setResults: dictionary withTask: task];
+ [dictionary setObject:responseObject forKey:@"data"];
+ CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary];
+ [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
+ } failure:^(NSURLSessionTask *task, NSError *error) {
+ NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
+ [self setResults: dictionary withTask: task];
+ NSString* errResponse = [[NSString alloc] initWithData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] encoding:NSUTF8StringEncoding];
+ [dictionary setObject:errResponse forKey:@"error"];
+ CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
+ [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
+ }];
+}
+
- (void)head:(CDVInvokedUrlCommand*)command {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.securityPolicy = securityPolicy;
diff --git a/www/cordovaHTTP.js b/www/cordovaHTTP.js
index 5fa69a7..2800962 100644
--- a/www/cordovaHTTP.js
+++ b/www/cordovaHTTP.js
@@ -103,13 +103,13 @@ var http = {
data = data || {};
headers = headers || {};
headers = mergeHeaders(this.headers, headers);
- return exec(success, failure, 'CordovaHttpPlugin', 'post', [url, data, this.dataSerializer, headers]);
+ return exec(success, failure, 'CordovaHttpPlugin', 'put', [url, data, this.dataSerializer, headers]);
},
delete: function (url, params, headers, success, failure) {
params = params || {};
headers = headers || {};
headers = mergeHeaders(this.headers, headers);
- return exec(success, failure, 'CordovaHttpPlugin', 'post', [url, params, this.dataSerializer, headers]);
+ return exec(success, failure, 'CordovaHttpPlugin', 'delete', [url, params, headers]);
},
head: function (url, params, headers, success, failure) {
headers = mergeHeaders(this.headers, headers);