Example usage for com.squareup.okhttp Headers.Builder get

List of usage examples for com.squareup.okhttp Headers.Builder get

Introduction

In this page you can find the example usage for com.squareup.okhttp Headers.Builder get.

Prototype

public String get(String name) 

Source Link

Document

Returns the last value corresponding to the specified field, or null.

Usage

From source file:com.facebook.react.modules.network.NetworkingModule.java

License:Open Source License

/**
 * Extracts the headers from the Array. If the format is invalid, this method will return null.
 *//*from   w w  w  .j  av a  2 s  .  c o m*/
private @Nullable Headers extractHeaders(@Nullable ReadableArray headersArray,
        @Nullable ReadableMap requestData) {
    if (headersArray == null) {
        return null;
    }
    Headers.Builder headersBuilder = new Headers.Builder();
    for (int headersIdx = 0, size = headersArray.size(); headersIdx < size; headersIdx++) {
        ReadableArray header = headersArray.getArray(headersIdx);
        if (header == null || header.size() != 2) {
            return null;
        }
        String headerName = header.getString(0);
        String headerValue = header.getString(1);
        headersBuilder.add(headerName, headerValue);
    }
    if (headersBuilder.get(USER_AGENT_HEADER_NAME) == null && mDefaultUserAgent != null) {
        headersBuilder.add(USER_AGENT_HEADER_NAME, mDefaultUserAgent);
    }

    // Sanitize content encoding header, supported only when request specify payload as string
    boolean isGzipSupported = requestData != null && requestData.hasKey(REQUEST_BODY_KEY_STRING);
    if (!isGzipSupported) {
        headersBuilder.removeAll(CONTENT_ENCODING_HEADER_NAME);
    }

    return headersBuilder.build();
}