public Builder method(String method, @Nullable 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; this.body = body; return this; }
static Request buildRequest(HttpHeaders headers, byte[] content, URI uri, HttpMethod method) throws MalformedURLException { okhttp3.MediaType contentType = getContentType(headers); RequestBody body = (content.length > 0 || okhttp3.internal.http.HttpMethod.requiresRequestBody(method.name()) ? RequestBody.create(contentType, content) : null); Request.Builder builder = new Request.Builder().url(uri.toURL()).method(method.name(), body); headers.forEach((headerName, headerValues) -> { for (String headerValue : headerValues) { builder.addHeader(headerName, headerValue); } }); return builder.build(); }
/** * Creates an OkHttp {@link Request} from the supplied information. * * <p>This method allows a {@code null} value for {@code requestHeaders} for situations where a * connection is already connected and access to the headers has been lost. See {@link * java.net.HttpURLConnection#getRequestProperties()} for details. */ public static Request createOkRequest( URI uri, String requestMethod, Map<String, List<String>> requestHeaders) { // OkHttp's Call API requires a placeholder body; the real body will be streamed separately. RequestBody placeholderBody = HttpMethod.requiresRequestBody(requestMethod) ? Util.EMPTY_REQUEST : null; Request.Builder builder = new Request.Builder() .url(uri.toString()) .method(requestMethod, placeholderBody); if (requestHeaders != null) { Headers headers = extractOkHeaders(requestHeaders, null); builder.headers(headers); } return builder.build(); }
public Builder method(String method, @Nullable 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; this.body = body; return this; }
static Request buildRequest(HttpHeaders headers, byte[] content, URI uri, HttpMethod method) throws MalformedURLException { okhttp3.MediaType contentType = getContentType(headers); RequestBody body = (content.length > 0 || okhttp3.internal.http.HttpMethod.requiresRequestBody(method.name()) ? RequestBody.create(contentType, content) : null); Request.Builder builder = new Request.Builder().url(uri.toURL()).method(method.name(), body); headers.forEach((headerName, headerValues) -> { for (String headerValue : headerValues) { builder.addHeader(headerName, headerValue); } }); return builder.build(); }
@Override public ServerResponse getResponse(final RequestBodyDelegate delegate) throws IOException { if (HttpMethod.permitsRequestBody(mMethod) || HttpMethod.requiresRequestBody(mMethod)) { RequestBody body = new RequestBody() { @Override public long contentLength() throws IOException { return mBodyLength; } @Override public MediaType contentType() { if (mContentType == null) return null; return MediaType.parse(mContentType); } @Override public void writeTo(BufferedSink sink) throws IOException { final OkHttpBodyWriter bodyWriter = new OkHttpBodyWriter(sink); delegate.onBodyReady(bodyWriter); bodyWriter.flush(); } }; mRequestBuilder.method(mMethod, body); } else { mRequestBuilder.method(mMethod, null); } mResponse = mClient.newCall(mRequestBuilder.build()).execute(); return new ServerResponse(mResponse.code(), mResponse.body().bytes(), getServerResponseHeaders(mResponse.headers())); }
RequestBody placeholderBody = HttpMethod.requiresRequestBody(requestMethod) ? Util.EMPTY_REQUEST : null;
RequestBody body = apiRequest.getBody(); if (body == null && HttpMethod.requiresRequestBody(method)) body = EMPTY_BODY;
public static boolean permitsRequestBody(String method) { return requiresRequestBody(method) || method.equals("OPTIONS") || method.equals("DELETE") // Permitted as spec is ambiguous. || method.equals("PROPFIND") // (WebDAV) without body: request <allprop/> || method.equals("MKCOL") // (WebDAV) may contain a body, but behaviour is unspecified || method.equals("LOCK"); // (WebDAV) body: create lock, without body: refresh lock }
public static boolean permitsRequestBody(String method) { return requiresRequestBody(method) || method.equals("OPTIONS") || method.equals("DELETE") // Permitted as spec is ambiguous. || method.equals("PROPFIND") // (WebDAV) without body: request <allprop/> || method.equals("MKCOL") // (WebDAV) may contain a body, but behaviour is unspecified || method.equals("LOCK"); // (WebDAV) body: create lock, without body: refresh lock }
public static boolean permitsRequestBody(String method) { return requiresRequestBody(method) || method.equals("OPTIONS") || method.equals("DELETE") // Permitted as spec is ambiguous. || method.equals("PROPFIND") // (WebDAV) without body: request <allprop/> || method.equals("MKCOL") // (WebDAV) may contain a body, but behaviour is unspecified || method.equals("LOCK"); // (WebDAV) body: create lock, without body: refresh lock }
@Override protected RequestBody buildRequestBody() { if (requestBody == null && TextUtils.isEmpty(content) && HttpMethod.requiresRequestBody(method)) { Exceptions.illegalArgument("requestBody and content can not be null in method:" + method); } if (requestBody == null && !TextUtils.isEmpty(content)) { requestBody = RequestBody.create(MEDIA_TYPE_PLAIN, content); } return requestBody; }
public Builder method(String method, RequestBody body) { if (method == null || method.length() == 0) { throw new IllegalArgumentException("method == null || 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; this.body = body; return this; }
public Builder method(String method, @Nullable 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; this.body = body; return this; }
public Builder method(String method, @Nullable 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; this.body = body; return this; }
public Builder method(String method, RequestBody body) { if (method == null || method.length() == 0) { throw new IllegalArgumentException("method == null || 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; this.body = body; return this; }
public Builder method(String method, RequestBody body) { if (method == null || method.length() == 0) { throw new IllegalArgumentException("method == null || 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; this.body = body; return this; }
static Request buildRequest(HttpHeaders headers, byte[] content, URI uri, HttpMethod method) throws MalformedURLException { okhttp3.MediaType contentType = getContentType(headers); RequestBody body = (content.length > 0 || okhttp3.internal.http.HttpMethod.requiresRequestBody(method.name()) ? RequestBody.create(contentType, content) : null); Request.Builder builder = new Request.Builder().url(uri.toURL()).method(method.name(), body); for (Map.Entry<String, List<String>> entry : headers.entrySet()) { String headerName = entry.getKey(); for (String headerValue : entry.getValue()) { builder.addHeader(headerName, headerValue); } } return builder.build(); }
static Request buildRequest(HttpHeaders headers, byte[] content, URI uri, HttpMethod method) throws MalformedURLException { okhttp3.MediaType contentType = getContentType(headers); RequestBody body = (content.length > 0 || okhttp3.internal.http.HttpMethod.requiresRequestBody(method.name()) ? RequestBody.create(contentType, content) : null); Request.Builder builder = new Request.Builder().url(uri.toURL()).method(method.name(), body); headers.forEach((headerName, headerValues) -> { for (String headerValue : headerValues) { builder.addHeader(headerName, headerValue); } }); return builder.build(); }
static Request buildRequest(HttpHeaders headers, byte[] content, URI uri, HttpMethod method) throws MalformedURLException { okhttp3.MediaType contentType = getContentType(headers); RequestBody body = (content.length > 0 || okhttp3.internal.http.HttpMethod.requiresRequestBody(method.name()) ? RequestBody.create(contentType, content) : null); Request.Builder builder = new Request.Builder().url(uri.toURL()).method(method.name(), body); headers.forEach((headerName, headerValues) -> { for (String headerValue : headerValues) { builder.addHeader(headerName, headerValue); } }); return builder.build(); }