Executes the specified PUT request. @param status status code to check @param method HTTP method @param is input stream (can be null) @param type media type (optional, may be null) @param path path of request @param params query parameters (keys and values) @return string result @thr
(final int status, final String method, final InputStream is,
final MediaType type, final String path, final Object... params)
| 199 | * @throws IOException I/O exception |
| 200 | */ |
| 201 | protected static String send(final int status, final String method, final InputStream is, |
| 202 | final MediaType type, final String path, final Object... params) throws IOException { |
| 203 | |
| 204 | final BodyPublisher pub = is != null ? HttpRequest.BodyPublishers.ofInputStream(() -> is) : |
| 205 | HttpRequest.BodyPublishers.noBody(); |
| 206 | |
| 207 | final StringBuilder sb = new StringBuilder(rootUrl + path); |
| 208 | final int pl = params.length; |
| 209 | for(int p = 0; p < pl; p += 2) { |
| 210 | sb.append(p == 0 ? '?' : '&').append(params[p]).append('='). |
| 211 | append(URLEncoder.encode(params[p + 1].toString(), StandardCharsets.UTF_8)); |
| 212 | } |
| 213 | final URI uri = URI.create(sb.toString()); |
| 214 | final HttpRequest.Builder builder = HttpRequest.newBuilder(uri).method(method, pub); |
| 215 | if(type != null) builder.setHeader("Content-Type", type.toString()); |
| 216 | |
| 217 | try { |
| 218 | final HttpClient client = IOUrl.client(true); |
| 219 | final HttpResponse<String> response = client.send(builder.build(), |
| 220 | HttpResponse.BodyHandlers.ofString()); |
| 221 | final String body = response.body(); |
| 222 | assertEquals(status, response.statusCode(), method + ' ' + path + "\nResponse: " + body); |
| 223 | return body; |
| 224 | } catch(final InterruptedException ex) { |
| 225 | throw new IOException(ex); |
| 226 | } |
| 227 | } |
| 228 | } |
no test coverage detected