FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Adding collection support · ws-java/unirest-java@9882fc9 · GitHub

Commit 9882fc9

Browse files
committed
Adding collection support
1 parent 408daab commit 9882fc9

3 files changed

Lines changed: 38 additions & 0 deletions

File tree

‎src/main/java/com/mashape/unirest/request/HttpRequestWithBody.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ a copy of this software and associated documentation files (the
2626
package com.mashape.unirest.request;
2727

2828
import java.io.File;
29+
import java.util.Collection;
2930
import java.util.Map;
3031
import java.util.Map.Entry;
3132

@@ -72,6 +73,12 @@ public HttpRequestWithBody queryString(String name, Object value) {
7273
return (HttpRequestWithBody) super.queryString(name, value);
7374
}
7475

76+
public MultipartBody field(String name, Collection<?> value) {
77+
MultipartBody body = new MultipartBody(this).field(name, value);
78+
this.body = body;
79+
return body;
80+
}
81+
7582
public MultipartBody field(String name, Object value) {
7683
MultipartBody body = new MultipartBody(this).field(name, (value == null) ? "" : value.toString());
7784
this.body = body;

‎src/main/java/com/mashape/unirest/request/body/MultipartBody.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ a copy of this software and associated documentation files (the
2929
import java.io.UnsupportedEncodingException;
3030
import java.nio.charset.Charset;
3131
import java.util.ArrayList;
32+
import java.util.Collection;
3233
import java.util.HashMap;
3334
import java.util.LinkedList;
3435
import java.util.List;
@@ -62,6 +63,14 @@ public MultipartBody field(String name, String value) {
6263
return field(name, value, false);
6364
}
6465

66+
public MultipartBody field(String name, Collection<?> collection) {
67+
for(Object current : collection) {
68+
boolean isFile = current instanceof File;
69+
field(name, current, isFile);
70+
}
71+
return this;
72+
}
73+
6574
public MultipartBody field(String name, Object value) {
6675
return field(name, value, false);
6776
}

‎src/test/java/com/mashape/unirest/test/http/UnirestTest.java‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ a copy of this software and associated documentation files (the
3636
import java.net.InetAddress;
3737
import java.net.URISyntaxException;
3838
import java.net.UnknownHostException;
39+
import java.util.Arrays;
3940
import java.util.concurrent.CountDownLatch;
4041
import java.util.concurrent.ExecutionException;
4142
import java.util.concurrent.ExecutorService;
@@ -56,6 +57,7 @@ a copy of this software and associated documentation files (the
5657
import com.mashape.unirest.http.async.Callback;
5758
import com.mashape.unirest.http.exceptions.UnirestException;
5859
import com.mashape.unirest.http.options.Options;
60+
import com.mashape.unirest.request.GetRequest;
5961

6062
public class UnirestTest {
6163

@@ -151,6 +153,13 @@ public void testPostRawBody() throws UnirestException, URISyntaxException, IOExc
151153
public void testCustomUserAgent() throws JSONException, UnirestException {
152154
HttpResponse<JsonNode> response = Unirest.get("http://httpbin.org/get?name=mark").header("user-agent", "hello-world").asJson();
153155
assertEquals("hello-world", response.getBody().getObject().getJSONObject("headers").getString("User-Agent"));
156+
157+
158+
GetRequest getRequest = Unirest.get("http");
159+
for(Object current : Arrays.asList(0, 1, 2)) {
160+
getRequest.queryString("name", current);
161+
}
162+
154163
}
155164

156165
@Test
@@ -575,4 +584,17 @@ public void testPostArray() throws JSONException, UnirestException {
575584
assertEquals("Tom", names.getString(1));
576585
}
577586

587+
@Test
588+
public void testPostCollection() throws JSONException, UnirestException {
589+
HttpResponse<JsonNode> response = Unirest.post("http://httpbin.org/post").field("name", Arrays.asList("Mark", "Tom")).asJson();
590+
591+
System.out.println(response.getBody().toString());
592+
593+
JSONArray names = response.getBody().getObject().getJSONObject("form").getJSONArray("name");
594+
assertEquals(2, names.length());
595+
596+
assertEquals("Mark", names.getString(0));
597+
assertEquals("Tom", names.getString(1));
598+
}
599+
578600
}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL