| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 408daab commit 9882fc9
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -26,6 +26,7 @@ a copy of this software and associated documentation files (the | |||
| 26 | 26 | package com.mashape.unirest.request; | |
| 27 | 27 | ||
| 28 | 28 | import java.io.File; | |
| 29 | + import java.util.Collection; | ||
| 29 | 30 | import java.util.Map; | |
| 30 | 31 | import java.util.Map.Entry; | |
| 31 | 32 | ||
@@ -72,6 +73,12 @@ public HttpRequestWithBody queryString(String name, Object value) { | |||
| 72 | 73 | return (HttpRequestWithBody) super.queryString(name, value); | |
| 73 | 74 | } | |
| 74 | 75 | ||
| 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 | + | ||
| 75 | 82 | public MultipartBody field(String name, Object value) { | |
| 76 | 83 | MultipartBody body = new MultipartBody(this).field(name, (value == null) ? "" : value.toString()); | |
| 77 | 84 | this.body = body; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -29,6 +29,7 @@ a copy of this software and associated documentation files (the | |||
| 29 | 29 | import java.io.UnsupportedEncodingException; | |
| 30 | 30 | import java.nio.charset.Charset; | |
| 31 | 31 | import java.util.ArrayList; | |
| 32 | + import java.util.Collection; | ||
| 32 | 33 | import java.util.HashMap; | |
| 33 | 34 | import java.util.LinkedList; | |
| 34 | 35 | import java.util.List; | |
@@ -62,6 +63,14 @@ public MultipartBody field(String name, String value) { | |||
| 62 | 63 | return field(name, value, false); | |
| 63 | 64 | } | |
| 64 | 65 | ||
| 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 | + | ||
| 65 | 74 | public MultipartBody field(String name, Object value) { | |
| 66 | 75 | return field(name, value, false); | |
| 67 | 76 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -36,6 +36,7 @@ a copy of this software and associated documentation files (the | |||
| 36 | 36 | import java.net.InetAddress; | |
| 37 | 37 | import java.net.URISyntaxException; | |
| 38 | 38 | import java.net.UnknownHostException; | |
| 39 | + import java.util.Arrays; | ||
| 39 | 40 | import java.util.concurrent.CountDownLatch; | |
| 40 | 41 | import java.util.concurrent.ExecutionException; | |
| 41 | 42 | import java.util.concurrent.ExecutorService; | |
@@ -56,6 +57,7 @@ a copy of this software and associated documentation files (the | |||
| 56 | 57 | import com.mashape.unirest.http.async.Callback; | |
| 57 | 58 | import com.mashape.unirest.http.exceptions.UnirestException; | |
| 58 | 59 | import com.mashape.unirest.http.options.Options; | |
| 60 | + import com.mashape.unirest.request.GetRequest; | ||
| 59 | 61 | ||
| 60 | 62 | public class UnirestTest { | |
| 61 | 63 | ||
@@ -151,6 +153,13 @@ public void testPostRawBody() throws UnirestException, URISyntaxException, IOExc | |||
| 151 | 153 | public void testCustomUserAgent() throws JSONException, UnirestException { | |
| 152 | 154 | HttpResponse<JsonNode> response = Unirest.get("http://httpbin.org/get?name=mark").header("user-agent", "hello-world").asJson(); | |
| 153 | 155 | 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 | + | ||
| 154 | 163 | } | |
| 155 | 164 | ||
| 156 | 165 | @Test | |
@@ -575,4 +584,17 @@ public void testPostArray() throws JSONException, UnirestException { | |||
| 575 | 584 | assertEquals("Tom", names.getString(1)); | |
| 576 | 585 | } | |
| 577 | 586 | ||
| 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 | + | ||
| 578 | 600 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments