Skip to content
This repository has been archived by the owner on Aug 4, 2019. It is now read-only.

Commit

Permalink
Url encoded post request closed #18, Multipart post request closed #19
Browse files Browse the repository at this point in the history
  • Loading branch information
EhsanMashhadi committed Nov 18, 2017
1 parent 8518939 commit 0bd5448
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
48 changes: 48 additions & 0 deletions app/src/androidTest/java/nuesoft/restfulpy/client/FormTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,52 @@ public void queryStringPost() {
}
}

@Test
public void urlEncodedPost() {

Map<String, Object> payload = new HashMap<>();
payload.put("item1", "value1");
final CountDownLatch countDownLatch = new CountDownLatch(1);
restAdapter = new RestAdapter("http://10.0.2.2:33415/", Authenticator.getAuthenticator());
restAdapter.request("echo", "post").setEncoding("urlencoded").addParameters(payload).send().then(result -> {

Assert.assertEquals(result.getBody().replaceAll("\\s+", ""), "{\"item1\":\"value1\"}");
countDownLatch.countDown();
}).fail(result -> {

result.printStackTrace();
countDownLatch.countDown();
});

try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

@Test
public void multipartPost() {

Map<String, Object> payload = new HashMap<>();
payload.put("item1", "value1");
payload.put("item2", "value2");
final CountDownLatch countDownLatch = new CountDownLatch(1);
restAdapter = new RestAdapter("http://10.0.2.2:33415/", Authenticator.getAuthenticator());
restAdapter.request("echo", "post").setEncoding("multipart").addParameters(payload).send().then(result -> {

Assert.assertEquals(result.getBody().replaceAll("\\s+", ""), "{\"item1\":\"value1\",\"item2\":\"value2\"}");
countDownLatch.countDown();
}).fail(result -> {

result.printStackTrace();
countDownLatch.countDown();
});

try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
22 changes: 20 additions & 2 deletions app/src/main/java/nuesoft/restfulpy/client/webService/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import nuesoft.restfulpy.client.exception.AuthenticationRequiredError;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
Expand All @@ -30,13 +31,16 @@ public class Request {

String resource = "session";
String verb = "get";
Object payload;
Map<String, Object> payload;
public Headers headers;
String encoding = "json";
RestAdapter restAdapter;
Map<String, Object> queryStringMap;

private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
private static final MediaType URL_ENCODED = MediaType.parse("application/x-www-form-urlencoded");
private static final MediaType MULTIPART_FORM = MediaType.parse("multipart/form-data");


public Request(RestAdapter restAdapter, String resource, String verb) {

Expand Down Expand Up @@ -139,7 +143,7 @@ public Request removeAuthenticationHeaders() {
}


public Request addParameters(Object payload) {
public Request addParameters(Map<String, Object> payload) {

this.payload = payload;
return this;
Expand Down Expand Up @@ -168,6 +172,20 @@ public Deferred<Response, Exception, Integer> send() {
requestBody = RequestBody.create(JSON, json);
break;
}

case "urlencoded": {
requestBody = RequestBody.create(URL_ENCODED, this.encodeQueryString(this.payload));
break;
}

case "multipart": {
FormBody.Builder formBody = new FormBody.Builder();
for (Map.Entry<String, Object> set : this.payload.entrySet()) {
formBody.add(set.getKey(), String.valueOf(set.getValue()));
}
requestBody = formBody.build();
break;
}
//TODO: other cased
//RequestBody requestBody1 = new FormBody.Builder().build();
//new MultipartBody.Builder().addFormDataPart()
Expand Down

0 comments on commit 0bd5448

Please sign in to comment.