|
| 1 | +## form-url-encoded-multipart |
| 2 | + |
| 3 | +This module provides support to `application/x-www-form-urlencoded` and `multipart/form-data` media types. |
| 4 | + |
| 5 | +## Install |
| 6 | + |
| 7 | +### Maven |
| 8 | +```xml |
| 9 | +<dependency> |
| 10 | + <groupId>com.github.ljtfreitas.julian-http-client</groupId> |
| 11 | + <artifactId>julian-http-client-form-url-encoded-multipart</artifactId> |
| 12 | + <version>${julian-http-client-version}</version> |
| 13 | +</dependency> |
| 14 | +``` |
| 15 | + |
| 16 | +### Gradle |
| 17 | +```kotlin |
| 18 | +dependencies { |
| 19 | + implementation("com.github.ljtfreitas.julian-http-client:julian-http-client-form-url-encoded-multipart:$julianHttpClientVersion") |
| 20 | +} |
| 21 | +``` |
| 22 | + |
| 23 | +Then, the required codecs will be registered automatically. Let's move to code :) |
| 24 | + |
| 25 | +## Usage |
| 26 | + |
| 27 | +### application/x-www-form-urlencoded |
| 28 | + |
| 29 | +`application/x-www-form-urlencoded` can be used to send form data in a request body. This module enables some ways to do that: |
| 30 | + |
| 31 | +```java |
| 32 | +import com.github.ljtfreitas.julian.Form; |
| 33 | + |
| 34 | +interface MyApi { |
| 35 | + |
| 36 | + // @FormUrlEncoded is an alias to @Body("application/x-www-form-urlencoded") |
| 37 | + |
| 38 | + @POST("/resource") |
| 39 | + @FormUrlEncoded |
| 40 | + void sendAsForm(@Body Form form); |
| 41 | + |
| 42 | + @POST("/resource") |
| 43 | + @FormUrlEncoded |
| 44 | + void sendAsMap(@Body Map<String, String> map); |
| 45 | + |
| 46 | + @POST("/resource") |
| 47 | + @FormUrlEncoded |
| 48 | + void sendAsMultiMap(@Body Map<String, Collection<String>> map); |
| 49 | +} |
| 50 | + |
| 51 | +MyApi myApi = new ProxyBuilder() |
| 52 | + .build(MyApi.class, "http://my.api.com"); |
| 53 | + |
| 54 | +// Form is a multi-map like, immutable object |
| 55 | +Form form = new Form() |
| 56 | + .join("name", "Tiago de Freitas Lima") |
| 57 | + .join("age", "36") |
| 58 | + .join("pets", "Zulu", "Puka", "Fiona"); // multiple values for the same field are valid |
| 59 | + |
| 60 | +myApi.sendAdForm(form); |
| 61 | + |
| 62 | +// We can use a plain Map too |
| 63 | +Map<String, String> map = new HashMap<>(); |
| 64 | +map.put("name", "Tiago de Freitas Lima"); |
| 65 | +map.put("age", "36"); |
| 66 | + |
| 67 | +myApi.sendAsMap(map); |
| 68 | + |
| 69 | +// Or we can use a map with multiple values |
| 70 | +Map<String, Collection<String>> multiMap = new HashMap<>(); |
| 71 | +multiMap.put("name", "Tiago de Freitas Lima"); |
| 72 | +multiMap.put("age", "36"); |
| 73 | +multiMap.put("pets", List.of("Zulu", "Puka, "Fiona"")); |
| 74 | + |
| 75 | +myApi.sendAsMultiMap(map); |
| 76 | +``` |
| 77 | + |
| 78 | +### multipart/form-data |
| 79 | + |
| 80 | +`multipart/form-data` is the media type used to upload files as well other fields as a form. |
| 81 | + |
| 82 | +We can send a multipart form using a `MultipartForm` object or a `Map`: |
| 83 | + |
| 84 | +```java |
| 85 | +import com.github.ljtfreitas.julian.multipart.MultipartForm; |
| 86 | + |
| 87 | +import java.io.File; |
| 88 | +import java.io.FileInputStream; |
| 89 | +import java.nio.ByteBuffer; |
| 90 | +import java.nio.file.Paths; |
| 91 | + |
| 92 | +interface MyApi { |
| 93 | + |
| 94 | + // @MultipartFormData is an alias to @Body("multipart/form-data") |
| 95 | + |
| 96 | + @POST("/resource") |
| 97 | + @MultipartFormData |
| 98 | + void uploadAsForm(@Body MultipartForm form); |
| 99 | + |
| 100 | + @POST("/resource") |
| 101 | + @MultipartFormData |
| 102 | + void sendAsMap(@Body Map<String, Object> map); |
| 103 | +} |
| 104 | + |
| 105 | +MyApi myApi = new ProxyBuilder() |
| 106 | + .build(MyApi.class, "http://my.api.com"); |
| 107 | + |
| 108 | +// MultipartForm is a multi-map like, immutable object |
| 109 | +Form form = new MultipartForm() |
| 110 | + .join(MultipartForm.Part.create("name", "Tiago de Freitas Lima")) |
| 111 | + .join(MultipartForm.Part.create("age", "36")) |
| 112 | + .join(MultipartForm.Part.create("picture", new File("/path/to/file.jpg"))) // we can send a java.io.File |
| 113 | + .join(MultipartForm.Part.create("picture", Paths.get("/path/to/file.jpg"), "file.jpg")) // or a java.nio.file.Path |
| 114 | + .join(MultipartForm.Part.create("picture", new FileInputStream("/path/to/file.jpg"), "file.jpg")) // or a java.io.InputStream |
| 115 | + .join(MultipartForm.Part.create("picture", new byte[...], "file.jpg")) // or a byte array with the file content |
| 116 | + .join(MultipartForm.Part.create("picture", ByteBuffer.wrap(new byte[...]), "file.jpg")); // or a java.nio.ByteBuffer with the file content |
| 117 | + |
| 118 | +myApi.sendAdForm(form); |
| 119 | + |
| 120 | +// We can use a plain Map too |
| 121 | +Map<String, Object> map = new HashMap<>(); |
| 122 | +map.put("name", "Tiago de Freitas Lima"); |
| 123 | +map.put("age", "36"); |
| 124 | +map.put("picture", new File("/path/to/file.jpg")); // the same types are supported: File, Path, etc... |
| 125 | + |
| 126 | +myApi.sendAsMap(map); |
| 127 | +``` |
0 commit comments