List of usage examples for com.squareup.okhttp Request.Builder headers
Headers headers
To view the source code for com.squareup.okhttp Request.Builder headers.
Click Source Link
From source file:com.cinchapi.concourse.http.HttpTest.java
License:Apache License
/** * Go through the {@code args} and filter out any that shouldn't be * considered URL args. It is possible that some of the filtered args will * be passed as arguments to the {@code builder}. * //w w w .ja v a 2s .c o m * @param builder * @param args * @return the filtered args */ private static Object[] filterArgs(Request.Builder builder, Object... args) { List<Object> argsList = Lists.newArrayList(args); Iterator<Object> it = argsList.iterator(); while (it.hasNext()) { Object arg = it.next(); if (arg instanceof Headers) { builder.headers((Headers) arg); it.remove(); } } args = argsList.size() != args.length ? argsList.toArray() : args; return args; }
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 w w w . j a v a2 s.com*/ } 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;/*w w w. j av 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.parse.ParseOkHttpClient.java
License:Open Source License
@Override /* package */ Request getRequest(ParseHttpRequest parseRequest) throws IOException { Request.Builder okHttpRequestBuilder = new Request.Builder(); ParseHttpRequest.Method method = parseRequest.getMethod(); // Set method switch (method) { case GET:/*w ww . jav a 2 s. c om*/ okHttpRequestBuilder.get(); break; case DELETE: okHttpRequestBuilder.delete(); break; case POST: case PUT: // Since we need to set body and method at the same time for POST and PUT, we will do it in // the following. break; default: // This case will never be reached since we have already handled this case in // ParseRequest.newRequest(). throw new IllegalStateException("Unsupported http method " + method.toString()); } // Set url okHttpRequestBuilder.url(parseRequest.getUrl()); // Set Header Headers.Builder okHttpHeadersBuilder = new Headers.Builder(); for (Map.Entry<String, String> entry : parseRequest.getAllHeaders().entrySet()) { okHttpHeadersBuilder.add(entry.getKey(), entry.getValue()); } // OkHttp automatically add gzip header so we do not need to deal with it Headers okHttpHeaders = okHttpHeadersBuilder.build(); okHttpRequestBuilder.headers(okHttpHeaders); // Set Body ParseHttpBody parseBody = parseRequest.getBody(); ParseOkHttpRequestBody okHttpRequestBody = null; if (parseBody instanceof ParseByteArrayHttpBody) { okHttpRequestBody = new ParseOkHttpRequestBody(parseBody); } switch (method) { case PUT: okHttpRequestBuilder.put(okHttpRequestBody); break; case POST: okHttpRequestBuilder.post(okHttpRequestBody); break; } return okHttpRequestBuilder.build(); }
From source file:org.wso2.carbon.identity.authenticator.duo.DuoHttp.java
License:Open Source License
public Response executeHttpRequest() throws Exception { String url = "https://" + host + uri; String queryString = createQueryString(); Request.Builder builder = new Request.Builder(); if (method.equals("POST")) { builder.post(RequestBody.create(FORM_ENCODED, queryString)); } else if (method.equals("PUT")) { builder.put(RequestBody.create(FORM_ENCODED, queryString)); } else if (method.equals("GET")) { if (queryString.length() > 0) { url += "?" + queryString; }//from ww w .j av a 2 s . com builder.get(); } else if (method.equals("DELETE")) { if (queryString.length() > 0) { url += "?" + queryString; } builder.delete(); } else { throw new UnsupportedOperationException("Unsupported method: " + method); } Request request = builder.url(url).build(); // Set up client. OkHttpClient httpclient = new OkHttpClient(); if (proxy != null) { httpclient.setProxy(proxy); } httpclient.setConnectTimeout(timeout, TimeUnit.SECONDS); httpclient.setWriteTimeout(timeout, TimeUnit.SECONDS); httpclient.setReadTimeout(timeout, TimeUnit.SECONDS); // finish and execute request builder.headers(headers.build()); return httpclient.newCall(builder.build()).execute(); }