| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,6 +12,7 @@ rightmenu: true | |||
| 12 | 12 | * [Basic Authentication](#basic-authentication) | |
| 13 | 13 | * [Body Data](#body-data) | |
| 14 | 14 | * [Entity Bodies](#entity-bodies) | |
| 15 | + * [JSON Patch Bodies](#json-patch-bodies) | ||
| 15 | 16 | * [Basic Forms](#basic-forms) | |
| 16 | 17 | * [File Uploads](#file-uploads) | |
| 17 | 18 | * [Object Mappers](#object-mappers) | |
@@ -87,7 +88,7 @@ Unirest.get("http://httpbin.org") | |||
| 87 | 88 | ||
| 88 | 89 | ## Headers | |
| 89 | 90 | Request headers can be added with the ```header``` method. | |
| 90 | - ``` | ||
| 91 | + ```java | ||
| 91 | 92 | Unirest.get("http://httpbin.org") | |
| 92 | 93 | .header("Accept", "application/json") | |
| 93 | 94 | .header("x-custom-header", "hello") | |
@@ -98,7 +99,7 @@ Unirest.get("http://httpbin.org") | |||
| 98 | 99 | Unirest exposes a shortcut for doing basic auth when you need to. Unirest handles the Base64 encoding part. | |
| 99 | 100 | Please make sure you are always doing this over HTTPS! | |
| 100 | 101 | ||
| 101 | - ``` | ||
| 102 | + ```java | ||
| 102 | 103 | Unirest.get("http://httpbin.org") | |
| 103 | 104 | .basicAuth("user", "password1!") | |
| 104 | 105 | .asString(); | |
@@ -135,6 +136,31 @@ Unirest.post("http://httpbin.org") | |||
| 135 | 136 | // This will use Jackson to serialize the object into JSON. | |
| 136 | 137 | ``` | |
| 137 | 138 | ||
| 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 | + | ||
| 138 | 164 | ### Basic Forms | |
| 139 | 165 | Basic http name value body params can be passed with simple field calls. | |
| 140 | 166 | 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. | |||
| 154 | 180 | ||
| 155 | 181 | The ```Content-Type``` for this type of request is defaulted to ```multipart/form-data``` | |
| 156 | 182 | ||
| 157 | - ``` | ||
| 183 | + ```java | ||
| 158 | 184 | Unirest.post("http://httpbin.org") | |
| 159 | 185 | .field("upload", new File("/MyFile.zip")) | |
| 160 | 186 | .asEmpty(); | |
@@ -163,7 +189,7 @@ Unirest.post("http://httpbin.org") | |||
| 163 | 189 | For large files you may want to use a InputStream. Pass it a file name if you want one. | |
| 164 | 190 | We are using a FileInputStream here but it can actually be any kind of InputStream. | |
| 165 | 191 | ||
| 166 | - ``` | ||
| 192 | + ```java | ||
| 167 | 193 | InputStream file = new FileInputStream(new File("/MyFile.zip")); | |
| 168 | 194 | ||
| 169 | 195 | Unirest.post("http://httpbin.org") | |
@@ -251,59 +277,6 @@ or consumers: | |||
| 251 | 277 | }); | |
| 252 | 278 | ``` | |
| 253 | 279 | ||
| 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 | - | ||
| 307 | 280 | # Configuration | |
| 308 | 281 | 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. | |
| 309 | 282 | All configuration is now done through ```Unirest.config()``` | |
| Back | FazBrowse Home | New Git URL |
0 commit comments