Skip to content

Commit 9a2ffd7

Browse files
committed
Added Input Stream and Byte Stream request sample code snippets.
1 parent f41cf45 commit 9a2ffd7

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,28 @@ HttpResponse<JsonNode> jsonResponse = Unirest.post("http://httpbin.org/post")
165165
.asJson();
166166
```
167167

168+
## Byte Stream as Entity Body
169+
170+
```java
171+
final InputStream stream = new FileInputStream(new File(getClass().getResource("/image.jpg").toURI()));
172+
final byte[] bytes = new byte[stream.available()];
173+
stream.read(bytes);
174+
stream.close();
175+
final HttpResponse<JsonNode> jsonResponse = Unirest.post("http://httpbin.org/post")
176+
.field("name", "Mark")
177+
.field("file", bytes, "image.jpg")
178+
.asJson();
179+
```
180+
181+
## InputStream as Entity Body
182+
183+
```java
184+
HttpResponse<JsonNode> jsonResponse = Unirest.post("http://httpbin.org/post")
185+
.field("name", "Mark")
186+
.field("file", new FileInputStream(new File(getClass().getResource("/image.jpg").toURI())), ContentType.APPLICATION_OCTET_STREAM, "image.jpg")
187+
.asJson();
188+
```
189+
168190
## Basic Authentication
169191
Authenticating the request with basic authentication can be done by calling the `basicAuth(username, password)` function:
170192
```java

0 commit comments

Comments
 (0)