List of usage examples for com.squareup.okhttp Request.Builder method
String method
To view the source code for com.squareup.okhttp Request.Builder method.
Click Source Link
From source file:abtlibrary.utils.as24ApiClient.ApiClient.java
License:Apache License
/** * Build HTTP call with the given options. * * @param path The sub-path of the HTTP URL * @param method The request method, one of "GET", "HEAD", "OPTIONS", "POST", "PUT", "PATCH" and "DELETE" * @param queryParams The query parameters * @param body The request body object//from w ww. j a va 2s . com * @param headerParams The header parameters * @param formParams The form parameters * @param authNames The authentications to apply * @param progressRequestListener Progress request listener * @return The HTTP call * @throws ApiException If fail to serialize the request body object */ public Call buildCall(String path, String method, List<Pair> queryParams, Object body, Map<String, String> headerParams, Map<String, Object> formParams, String[] authNames, ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException { updateParamsForAuth(authNames, queryParams, headerParams); final String url = buildUrl(path, queryParams); final Request.Builder reqBuilder = new Request.Builder().url(url); processHeaderParams(headerParams, reqBuilder); String contentType = (String) headerParams.get("Content-Type"); // ensuring a default content type if (contentType == null) { contentType = "application/json"; } RequestBody reqBody; if (!HttpMethod.permitsRequestBody(method)) { reqBody = null; } else if ("application/x-www-form-urlencoded".equals(contentType)) { reqBody = buildRequestBodyFormEncoding(formParams); } else if ("multipart/form-data".equals(contentType)) { reqBody = buildRequestBodyMultipart(formParams); } else if (body == null) { if ("DELETE".equals(method)) { // allow calling DELETE without sending a request body reqBody = null; } else { // use an empty request body (for POST, PUT and PATCH) reqBody = RequestBody.create(MediaType.parse(contentType), ""); } } else { reqBody = serialize(body, contentType); } Request request = null; if (progressRequestListener != null && reqBody != null) { ProgressRequestBody progressRequestBody = new ProgressRequestBody(reqBody, progressRequestListener); request = reqBuilder.method(method, progressRequestBody).build(); } else { request = reqBuilder.method(method, reqBody).build(); } return httpClient.newCall(request); }
From source file:com.aix.city.comm.OkHttpStack.java
License:Open Source License
@SuppressWarnings("deprecation") private static void setConnectionParametersForRequest(com.squareup.okhttp.Request.Builder builder, Request<?> request) throws IOException, AuthFailureError { switch (request.getMethod()) { case Request.Method.DEPRECATED_GET_OR_POST: // Ensure backwards compatibility. Volley assumes a request with a null body is a GET. byte[] postBody = request.getPostBody(); if (postBody != null) { builder.post(RequestBody.create(MediaType.parse(request.getPostBodyContentType()), postBody)); }/* w ww.j av a2s . c om*/ break; case Request.Method.GET: builder.get(); break; case Request.Method.DELETE: builder.delete(); break; case Request.Method.POST: builder.post(createRequestBody(request)); break; case Request.Method.PUT: builder.put(createRequestBody(request)); break; case Request.Method.HEAD: builder.head(); break; case Request.Method.OPTIONS: builder.method("OPTIONS", null); break; case Request.Method.TRACE: builder.method("TRACE", null); break; case Request.Method.PATCH: builder.patch(createRequestBody(request)); break; default: throw new IllegalStateException("Unknown method type."); } }
From source file:com.blackcat.coach.net.OkHttpStack.java
License:Open Source License
@SuppressWarnings("deprecation") private static void setConnectionParametersForRequest(com.squareup.okhttp.Request.Builder builder, Request<?> request) throws IOException, AuthFailureError { switch (request.getMethod()) { case Request.Method.DEPRECATED_GET_OR_POST: // Ensure backwards compatibility. Volley assumes a request with a null body is a GET. byte[] postBody = request.getPostBody(); if (postBody != null) { builder.post(RequestBody.create(MediaType.parse(request.getBodyContentType()), postBody)); }//from w w w. jav a2 s. com break; case Request.Method.GET: builder.get(); break; case Request.Method.DELETE: builder.delete(); break; case Request.Method.POST: builder.post(createRequestBody(request)); break; case Request.Method.PUT: builder.put(createRequestBody(request)); break; case Request.Method.HEAD: builder.head(); break; case Request.Method.OPTIONS: builder.method("OPTIONS", null); break; case Request.Method.TRACE: builder.method("TRACE", null); break; case Request.Method.PATCH: builder.patch(createRequestBody(request)); break; default: throw new IllegalStateException("Unknown method type."); } }
From source file:com.cdancy.artifactory.rest.config.ArtifactoryOkHttpCommandExecutorService.java
License:Apache License
@Override protected Request convert(HttpRequest request) throws IOException, InterruptedException { Request.Builder builder = new Request.Builder(); builder.url(request.getEndpoint().toString()); populateHeaders(request, builder);/*from w w w . j a v a 2 s .co m*/ RequestBody body = null; Payload payload = request.getPayload(); if (payload != null) { Long length = checkNotNull(payload.getContentMetadata().getContentLength(), "payload.getContentLength"); if (length > 0) { body = generateRequestBody(request, payload); } } builder.method(request.getMethod(), body); return builder.build(); }
From source file:com.datastore_android_sdk.okhttp.OkHttpStack.java
License:Open Source License
private static void setConnectionParametersForRequest(com.squareup.okhttp.Request.Builder builder, Request<?> request) throws IOException { switch (request.getMethod()) { case RxVolley.Method.GET: builder.get();//from w ww .j a v a 2 s.co m break; case RxVolley.Method.DELETE: builder.delete(); break; case RxVolley.Method.POST: builder.post(createRequestBody(request)); break; case RxVolley.Method.PUT: builder.put(createRequestBody(request)); break; case RxVolley.Method.HEAD: builder.head(); break; case RxVolley.Method.OPTIONS: builder.method("OPTIONS", null); break; case RxVolley.Method.TRACE: builder.method("TRACE", null); break; case RxVolley.Method.PATCH: builder.patch(createRequestBody(request)); break; default: throw new IllegalStateException("Unknown method type."); } }
From source file:com.facebook.react.modules.network.NetworkingModule.java
License:Open Source License
@ReactMethod public void sendRequest(String method, String url, int requestId, ReadableArray headers, ReadableMap data, final Callback callback) { // We need to call the callback to avoid leaking memory on JS even when input for sending // request is erroneous or insufficient. For non-http based failures we use code 0, which is // interpreted as a transport error. // Callback accepts following arguments: responseCode, headersString, responseBody Request.Builder requestBuilder = new Request.Builder().url(url); if (requestId != 0) { requestBuilder.tag(requestId);//from ww w . ja v a 2s . c om } Headers requestHeaders = extractHeaders(headers, data); if (requestHeaders == null) { callback.invoke(0, null, "Unrecognized headers format"); return; } String contentType = requestHeaders.get(CONTENT_TYPE_HEADER_NAME); String contentEncoding = requestHeaders.get(CONTENT_ENCODING_HEADER_NAME); requestBuilder.headers(requestHeaders); if (data == null) { requestBuilder.method(method, null); } else if (data.hasKey(REQUEST_BODY_KEY_STRING)) { if (contentType == null) { callback.invoke(0, null, "Payload is set but no content-type header specified"); return; } String body = data.getString(REQUEST_BODY_KEY_STRING); MediaType contentMediaType = MediaType.parse(contentType); if (RequestBodyUtil.isGzipEncoding(contentEncoding)) { RequestBody requestBody = RequestBodyUtil.createGzip(contentMediaType, body); if (requestBody == null) { callback.invoke(0, null, "Failed to gzip request body"); return; } requestBuilder.method(method, requestBody); } else { requestBuilder.method(method, RequestBody.create(contentMediaType, body)); } } else if (data.hasKey(REQUEST_BODY_KEY_URI)) { if (contentType == null) { callback.invoke(0, null, "Payload is set but no content-type header specified"); return; } String uri = data.getString(REQUEST_BODY_KEY_URI); InputStream fileInputStream = RequestBodyUtil.getFileInputStream(getReactApplicationContext(), uri); if (fileInputStream == null) { callback.invoke(0, null, "Could not retrieve file for uri " + uri); return; } requestBuilder.method(method, RequestBodyUtil.create(MediaType.parse(contentType), fileInputStream)); } else if (data.hasKey(REQUEST_BODY_KEY_FORMDATA)) { if (contentType == null) { contentType = "multipart/form-data"; } ReadableArray parts = data.getArray(REQUEST_BODY_KEY_FORMDATA); MultipartBuilder multipartBuilder = constructMultipartBody(parts, contentType, callback); if (multipartBuilder == null) { return; } requestBuilder.method(method, multipartBuilder.build()); } else { // Nothing in data payload, at least nothing we could understand anyway. // Ignore and treat it as if it were null. requestBuilder.method(method, null); } mClient.newCall(requestBuilder.build()).enqueue(new com.squareup.okhttp.Callback() { @Override public void onFailure(Request request, IOException e) { if (mShuttingDown) { return; } // We need to call the callback to avoid leaking memory on JS even when input for // sending request is erronous or insufficient. For non-http based failures we use // code 0, which is interpreted as a transport error callback.invoke(0, null, e.getMessage()); } @Override public void onResponse(Response response) throws IOException { if (mShuttingDown) { return; } String responseBody; try { responseBody = response.body().string(); } catch (IOException e) { // The stream has been cancelled or closed, nothing we can do callback.invoke(0, null, e.getMessage()); return; } WritableMap responseHeaders = Arguments.createMap(); Headers headers = response.headers(); for (int i = 0; i < headers.size(); i++) { String headerName = headers.name(i); // multiple values for the same header if (responseHeaders.hasKey(headerName)) { responseHeaders.putString(headerName, responseHeaders.getString(headerName) + ", " + headers.value(i)); } else { responseHeaders.putString(headerName, headers.value(i)); } } callback.invoke(response.code(), responseHeaders, responseBody); } }); }
From source file:com.ibm.mobilefirstplatform.clientsdk.android.core.internal.BaseRequest.java
License:Apache License
protected void sendRequest(final ResponseListener listener, final RequestBody requestBody) { if (method == null || !isValidMethod(method)) { listener.onFailure(null, new IllegalArgumentException("Method is not valid: " + method), null); return;/*from w w w . ja v a 2 s . c o m*/ } Request.Builder requestBuilder = new Request.Builder(); requestBuilder.headers(headers.build()); try { if (getQueryParamsMap().size() == 0) { requestBuilder.url(url); } else { requestBuilder.url(getURLWithQueryParameters(url, getQueryParamsMap())); } } catch (MalformedURLException e) { listener.onFailure(null, e, null); return; } //A GET request cannot have a body in OKHTTP if (!method.equalsIgnoreCase("GET")) { requestBuilder.method(method, requestBody); } else { requestBuilder.get(); } Request request = requestBuilder.build(); OkHttpClient client = getHttpClient(); client.newCall(request).enqueue(getCallback(listener)); client.newCall(request); }
From source file:com.ichg.service.volley.OkHttpStack.java
License:Open Source License
@SuppressWarnings("deprecation") private static void setConnectionParametersForRequest(com.squareup.okhttp.Request.Builder builder, Request<?> request) throws IOException, AuthFailureError { switch (request.getMethod()) { case Request.Method.DEPRECATED_GET_OR_POST: // Ensure backwards compatibility. Volley assumes a request with a null body is a GET. byte[] postBody = request.getPostBody(); if (postBody != null) { builder.post(RequestBody.create(MediaType.parse(request.getPostBodyContentType()), postBody)); }/*from w w w . j a v a 2 s . com*/ break; case Request.Method.GET: builder.get(); break; case Request.Method.DELETE: builder.delete(createRequestBody(request)); break; case Request.Method.POST: builder.post(createRequestBody(request)); break; case Request.Method.PUT: builder.put(createRequestBody(request)); break; case Request.Method.HEAD: builder.head(); break; case Request.Method.OPTIONS: builder.method("OPTIONS", null); break; case Request.Method.TRACE: builder.method("TRACE", null); break; case Request.Method.PATCH: builder.patch(createRequestBody(request)); break; default: throw new IllegalStateException("Unknown method type."); } }
From source file:com.oracle.bdcs.bdm.client.ApiClient.java
License:Apache License
/** * Build HTTP call with the given options. * * @param path The sub-path of the HTTP URL * @param method The request method, one of "GET", "HEAD", "OPTIONS", "POST", "PUT", "PATCH" and "DELETE" * @param queryParams The query parameters * @param body The request body object// w ww . ja v a 2 s . c o m * @param headerParams The header parameters * @param formParams The form parameters * @param authNames The authentications to apply * @param progressRequestListener Progress request listener * @return The HTTP call * @throws ApiException If fail to serialize the request body object */ public Call buildCall(String path, String method, List<Pair> queryParams, Object body, Map<String, String> headerParams, Map<String, Object> formParams, String[] authNames, ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException { String[] auths = { ApiKeyAuth.class.getName() }; updateParamsForAuth(auths, queryParams, headerParams); final String url = buildUrl(path, queryParams); final Request.Builder reqBuilder = new Request.Builder().url(url); processHeaderParams(headerParams, reqBuilder); String contentType = (String) headerParams.get("Content-Type"); // ensuring a default content type if (contentType == null) { contentType = "application/json"; } RequestBody reqBody; if (!HttpMethod.permitsRequestBody(method)) { reqBody = null; } else if ("application/x-www-form-urlencoded".equals(contentType)) { reqBody = buildRequestBodyFormEncoding(formParams); } else if ("multipart/form-data".equals(contentType)) { reqBody = buildRequestBodyMultipart(formParams); } else if (body == null) { if ("DELETE".equals(method)) { // allow calling DELETE without sending a request body reqBody = null; } else { // use an empty request body (for POST, PUT and PATCH) reqBody = RequestBody.create(MediaType.parse(contentType), ""); } } else { reqBody = serialize(body, contentType); } Request request = null; if (progressRequestListener != null && reqBody != null) { ProgressRequestBody progressRequestBody = new ProgressRequestBody(reqBody, progressRequestListener); request = reqBuilder.method(method, progressRequestBody).build(); } else { request = reqBuilder.method(method, reqBody).build(); } return httpClient.newCall(request); }
From source file:com.phattn.vnexpressnews.io.OkHttpStack.java
License:Open Source License
@SuppressWarnings("deprecation") private static void setConnectionParametersForRequest(com.squareup.okhttp.Request.Builder builder, Request<?> request) throws IOException, AuthFailureError { switch (request.getMethod()) { case Request.Method.DEPRECATED_GET_OR_POST: // Ensure backwards compatibility. Volley assumes a request with a null body is a GET byte[] postBody = request.getPostBody(); if (postBody != null) { builder.post(RequestBody.create(MediaType.parse(request.getPostBodyContentType()), postBody)); }// w w w .j a v a 2 s . co m break; case Request.Method.GET: builder.get(); break; case Request.Method.DELETE: builder.delete(); break; case Request.Method.POST: builder.post(createRequestBody(request)); break; case Request.Method.HEAD: builder.head(); break; case Request.Method.OPTIONS: builder.method("OPTIONS", null); break; case Request.Method.TRACE: builder.method("TRACE", null); break; case Request.Method.PATCH: builder.patch(createRequestBody(request)); break; default: throw new IllegalStateException("Unknown method type."); } }