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

more doc · java4dz/unirest-java@194237b · GitHub

Commit 194237b

Browse files
committed
more doc
1 parent 55ec7a0 commit 194237b

1 file changed

Lines changed: 30 additions & 57 deletions

File tree

‎docs/index.md‎

Lines changed: 30 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ rightmenu: true
1212
* [Basic Authentication](#basic-authentication)
1313
* [Body Data](#body-data)
1414
* [Entity Bodies](#entity-bodies)
15+
* [JSON Patch Bodies](#json-patch-bodies)
1516
* [Basic Forms](#basic-forms)
1617
* [File Uploads](#file-uploads)
1718
* [Object Mappers](#object-mappers)
@@ -87,7 +88,7 @@ Unirest.get("http://httpbin.org")
8788

8889
## Headers
8990
Request headers can be added with the ```header``` method.
90-
```
91+
```java
9192
Unirest.get("http://httpbin.org")
9293
.header("Accept", "application/json")
9394
.header("x-custom-header", "hello")
@@ -98,7 +99,7 @@ Unirest.get("http://httpbin.org")
9899
Unirest exposes a shortcut for doing basic auth when you need to. Unirest handles the Base64 encoding part.
99100
Please make sure you are always doing this over HTTPS!
100101

101-
```
102+
```java
102103
Unirest.get("http://httpbin.org")
103104
.basicAuth("user", "password1!")
104105
.asString();
@@ -135,6 +136,31 @@ Unirest.post("http://httpbin.org")
135136
// This will use Jackson to serialize the object into JSON.
136137
```
137138

139+
### JSON Patch Bodies
140+
Unirest has full native support for JSON Patch requests (RFC-6902 see http://jsonpatch.com/)
141+
```java
142+
Unirest.jsonPatch(MockServer.PATCH)
143+
.add("/fruits/-", "Apple")
144+
.remove("/bugs")
145+
.replace("/lastname", "Flintstone")
146+
.test("/firstname", "Fred")
147+
.move("/old/location", "/new/location")
148+
.copy("/original/location", "/new/location")
149+
.asJson();
150+
```
151+
will send a request with a body of
152+
```json
153+
[
154+
{"op":"add","path":"/fruits/-","value":"Apple"},
155+
{"op":"remove","path":"/bugs"},
156+
{"op":"replace","path":"/lastname","value":"Flintstone"},
157+
{"op":"test","path":"/firstname","value":"Fred"},
158+
{"op":"move","path":"/new/location","from":"/old/location"},
159+
{"op":"copy","path":"/new/location","from":"/original/location"}
160+
]
161+
162+
```
163+
138164
### Basic Forms
139165
Basic http name value body params can be passed with simple field calls.
140166
The ```Content-Type``` for this type of request is defaulted to ```application/x-www-form-urlencoded```
@@ -154,7 +180,7 @@ You can also post binary data in a form. Like a file.
154180

155181
The ```Content-Type``` for this type of request is defaulted to ```multipart/form-data```
156182

157-
```
183+
```java
158184
Unirest.post("http://httpbin.org")
159185
.field("upload", new File("/MyFile.zip"))
160186
.asEmpty();
@@ -163,7 +189,7 @@ Unirest.post("http://httpbin.org")
163189
For large files you may want to use a InputStream. Pass it a file name if you want one.
164190
We are using a FileInputStream here but it can actually be any kind of InputStream.
165191

166-
```
192+
```java
167193
InputStream file = new FileInputStream(new File("/MyFile.zip"));
168194

169195
Unirest.post("http://httpbin.org")
@@ -251,59 +277,6 @@ or consumers:
251277
});
252278
```
253279

254-
#### File Uploads
255-
Creating `multipart` requests with Java is trivial, simply pass along a `File` or an InputStream Object as a field:
256-
257-
```java
258-
HttpResponse<JsonNode> jsonResponse = Unirest.post("http://httpbin.org/post")
259-
.header("accept", "application/json")
260-
.field("parameter", "value")
261-
.field("file", new File("/tmp/file"))
262-
.asJson();
263-
```
264-
265-
#### Custom Entity Body
266-
267-
```java
268-
HttpResponse<JsonNode> jsonResponse = Unirest.post("http://httpbin.org/post")
269-
.header("accept", "application/json")
270-
.body("{\"parameter\":\"value\", \"foo\":\"bar\"}")
271-
.asJson();
272-
```
273-
274-
#### JSON Patch Requests
275-
Unirest has full native support for JSON Patch requests
276-
```java
277-
Unirest.jsonPatch(MockServer.PATCH)
278-
.add("/fruits/-", "Apple")
279-
.remove("/bugs")
280-
.replace("/lastname", "Flintstone")
281-
.test("/firstname", "Fred")
282-
.move("/old/location", "/new/location")
283-
.copy("/original/location", "/new/location")
284-
.asJson();
285-
```
286-
will send a request with a body of
287-
```json
288-
[
289-
{"op":"add","path":"/fruits/-","value":"Apple"},
290-
{"op":"remove","path":"/bugs"},
291-
{"op":"replace","path":"/lastname","value":"Flintstone"},
292-
{"op":"test","path":"/firstname","value":"Fred"},
293-
{"op":"move","path":"/new/location","from":"/old/location"},
294-
{"op":"copy","path":"/new/location","from":"/original/location"}
295-
]
296-
297-
```
298-
299-
#### Basic Authentication
300-
Authenticating the request with basic authentication can be done by calling the `basicAuth(username, password)` function:
301-
```java
302-
Unirest.get("http://httpbin.org/headers")
303-
.basicAuth("username", "password")
304-
.asJson();
305-
```
306-
307280
# Configuration
308281
Previous versions of unirest had configuration split across several different places. Sometimes it was done on ```Unirest```, sometimes it was done on ```Option```, sometimes it was somewhere else.
309282
All configuration is now done through ```Unirest.config()```

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL