@Override Request toImmutable() { Object body = body(); MediaType mediaType = MediaType.parse(contentType()); RequestBody requestBody = null; if (body != null) { requestBody = body instanceof String ? RequestBody.create(mediaType, (String) body) : RequestBody.create(mediaType, (byte[]) body); } else if (HttpMethod.requiresRequestBody(method)) { // The method required a body but none was given. Use an empty one. requestBody = RequestBody.create(mediaType, new byte[0]); } return new RecordedRequest.Builder() .headers(okhttp3.Headers.of(headers())) .method(method, requestBody) .url(HttpUrl.get(uri)) .build(); } }
Builder method(String method, RequestBody body) { if (method == null) throw new NullPointerException("method == null"); if (method.length() == 0) throw new IllegalArgumentException("method.length() == 0"); if (body != null && !HttpMethod.permitsRequestBody(method)) { throw new IllegalArgumentException("method " + method + " must not have a request body."); } if (body == null && HttpMethod.requiresRequestBody(method)) { throw new IllegalArgumentException("method " + method + " must have a request body."); } this.method = method; if (body != null) { try { Buffer buffer = new Buffer(); body.writeTo(buffer); this.body = buffer.readByteArray(); MediaType contentType = body.contentType(); if (contentType != null) { addHeader(CONTENT_TYPE, contentType.toString()); } } catch (IOException e) { throw new RuntimeException(e); } } return this; }
Builder delete() { return delete(RequestBody.create(null, new byte[0])); }
Builder method(String method, RequestBody body) { if (method == null) throw new NullPointerException("method == null"); if (method.length() == 0) throw new IllegalArgumentException("method.length() == 0"); if (body != null && !HttpMethod.permitsRequestBody(method)) { throw new IllegalArgumentException("method " + method + " must not have a request body."); } if (body == null && HttpMethod.requiresRequestBody(method)) { throw new IllegalArgumentException("method " + method + " must have a request body."); } this.method = method; if (body != null) { try { Buffer buffer = new Buffer(); body.writeTo(buffer); this.body = buffer.readByteArray(); MediaType contentType = body.contentType(); if (contentType != null) { addHeader(CONTENT_TYPE, contentType.toString()); } } catch (IOException e) { throw new RuntimeException(e); } } return this; }
/** * Sets the URL target of this request. * * @throws IllegalArgumentException if {@code url} is not a valid HTTP or HTTPS URL. Avoid this * exception by calling {@link HttpUrl#parse}; it returns null * for invalid URLs. */ Builder url(String url) { if (url == null) throw new NullPointerException("url == null"); // Silently replace websocket URLs with HTTP URLs. if (url.regionMatches(true, 0, "ws:", 0, 3)) { url = "http:" + url.substring(3); } else if (url.regionMatches(true, 0, "wss:", 0, 4)) { url = "https:" + url.substring(4); } HttpUrl parsed = HttpUrl.parse(url); if (parsed == null) throw new IllegalArgumentException("unexpected url: " + url); return url(parsed); }
/** * Sets the URL target of this request. * * @throws IllegalArgumentException if {@code url} is not a valid HTTP or HTTPS URL. Avoid this * exception by calling {@link HttpUrl#parse}; it returns null * for invalid URLs. */ Builder url(String url) { if (url == null) throw new NullPointerException("url == null"); // Silently replace websocket URLs with HTTP URLs. if (url.regionMatches(true, 0, "ws:", 0, 3)) { url = "http:" + url.substring(3); } else if (url.regionMatches(true, 0, "wss:", 0, 4)) { url = "https:" + url.substring(4); } HttpUrl parsed = HttpUrl.parse(url); if (parsed == null) throw new IllegalArgumentException("unexpected url: " + url); return url(parsed); }
@Override Request toImmutable() { Object body = body(); MediaType mediaType = MediaType.parse(contentType()); RequestBody requestBody = null; if (body != null) { requestBody = body instanceof String ? RequestBody.create(mediaType, (String) body) : RequestBody.create(mediaType, (byte[]) body); } else if (HttpMethod.requiresRequestBody(method)) { // The method required a body but none was given. Use an empty one. requestBody = RequestBody.create(mediaType, new byte[0]); } return new RecordedRequest.Builder() .headers(okhttp3.Headers.of(headers())) .method(method, requestBody) .url(HttpUrl.get(uri)) .build(); } }
Builder patch(RequestBody body) { return method("PATCH", body); }
/** * Sets the URL target of this request. * * @throws IllegalArgumentException if the scheme of {@code url} is not {@code http} or {@code * https}. */ Builder url(URL url) { if (url == null) throw new NullPointerException("url == null"); HttpUrl parsed = HttpUrl.get(url); if (parsed == null) throw new IllegalArgumentException("unexpected url: " + url); return url(parsed); }
Builder delete(RequestBody body) { return method("DELETE", body); }
@Override public Builder newBuilder() { return new Builder(this); }
Builder put(RequestBody body) { return method("PUT", body); }
Builder head() { return method("HEAD", null); }
Builder get() { return method("GET", null); }
Builder delete() { return delete(RequestBody.create(null, new byte[0])); }
Builder put(RequestBody body) { return method("PUT", body); }
Builder post(RequestBody body) { return method("POST", body); }
/** * Sets the URL target of this request. * * @throws IllegalArgumentException if the scheme of {@code url} is not {@code http} or {@code * https}. */ Builder url(URL url) { if (url == null) throw new NullPointerException("url == null"); HttpUrl parsed = HttpUrl.get(url); if (parsed == null) throw new IllegalArgumentException("unexpected url: " + url); return url(parsed); }
Builder post(RequestBody body) { return method("POST", body); }
Builder head() { return method("HEAD", null); }