Java Code Examples for okhttp3.internal.http.HttpMethod#permitsRequestBody()

The following examples show how to use okhttp3.internal.http.HttpMethod#permitsRequestBody() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ResponseHeaderRecord.java    From apollo-android with MIT License 6 votes vote down vote up
Response response() {
  RequestBody body = null;
  if (HttpMethod.permitsRequestBody(requestMethod)) {
    body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), "");
  }

  Request cacheRequest = new Request.Builder()
      .url(url)
      .method(requestMethod, body)
      .headers(varyHeaders)
      .build();
  return new Response.Builder()
      .request(cacheRequest)
      .protocol(protocol)
      .code(code)
      .message(message)
      .headers(responseHeaders)
      .handshake(handshake)
      .sentRequestAtMillis(sentRequestMillis)
      .receivedResponseAtMillis(receivedResponseMillis)
      .build();
}
 
Example 2
Source File: QSNormalRequestBody.java    From qingstor-sdk-java with Apache License 2.0 5 votes vote down vote up
@Override
public RequestBody getRequestBody(
        String contentType,
        long contentLength,
        String method,
        Map<String, Object> bodyParams,
        Map<String, Object> queryParams)
        throws QSException {
    log.debug("----QSNormalRequestBody----");
    MediaType mediaType = MediaType.parse(contentType);
    if (bodyParams != null && bodyParams.size() > 0) {

        RequestBody body = null;
        Object bodyObj = getBodyContent(bodyParams);
        if (bodyObj instanceof String) {
            body = RequestBody.create(mediaType, bodyObj.toString());
        } else if (bodyObj instanceof File) {
            body = RequestBody.create(mediaType, (File) bodyObj);
        } else if (bodyObj instanceof InputStream) {
            body = new InputStreamUploadBody(contentType, (InputStream) bodyObj, contentLength);
        }
        return body;
        // connection.getOutputStream().write(bodyContent.getBytes());
    } else {
        if (HttpMethod.permitsRequestBody(method)) {
            return new EmptyRequestBody(contentType);
        }
    }
    return null;
}
 
Example 3
Source File: ApiClient.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
/**
 * Build an HTTP request 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 collectionQueryParams The collection query parameters
 * @param body The request body object
 * @param headerParams The header parameters
 * @param cookieParams The cookie parameters
 * @param formParams The form parameters
 * @param authNames The authentications to apply
 * @param callback Callback for upload/download progress
 * @return The HTTP request
 * @throws ApiException If fail to serialize the request body object
 */
public Request buildRequest(String path, String method, List<Pair> queryParams, List<Pair> collectionQueryParams, Object body, Map<String, String> headerParams, Map<String, String> cookieParams, Map<String, Object> formParams, String[] authNames, ApiCallback callback) throws ApiException {
    updateParamsForAuth(authNames, queryParams, headerParams, cookieParams);

    final String url = buildUrl(path, queryParams, collectionQueryParams);
    final Request.Builder reqBuilder = new Request.Builder().url(url);
    processHeaderParams(headerParams, reqBuilder);
    processCookieParams(cookieParams, 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);
    }

    // Associate callback with request (if not null) so interceptor can
    // access it when creating ProgressResponseBody
    reqBuilder.tag(callback);

    Request request = null;

    if (callback != null && reqBody != null) {
        ProgressRequestBody progressRequestBody = new ProgressRequestBody(reqBody, callback);
        request = reqBuilder.method(method, progressRequestBody).build();
    } else {
        request = reqBuilder.method(method, reqBody).build();
    }

    return request;
}
 
Example 4
Source File: ApiClient.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
/**
 * Build an HTTP request 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 collectionQueryParams The collection query parameters
 * @param body The request body object
 * @param headerParams The header parameters
 * @param cookieParams The cookie parameters
 * @param formParams The form parameters
 * @param authNames The authentications to apply
 * @param callback Callback for upload/download progress
 * @return The HTTP request
 * @throws ApiException If fail to serialize the request body object
 */
public Request buildRequest(String path, String method, List<Pair> queryParams, List<Pair> collectionQueryParams, Object body, Map<String, String> headerParams, Map<String, String> cookieParams, Map<String, Object> formParams, String[] authNames, ApiCallback callback) throws ApiException {
    updateParamsForAuth(authNames, queryParams, headerParams, cookieParams);

    final String url = buildUrl(path, queryParams, collectionQueryParams);
    final Request.Builder reqBuilder = new Request.Builder().url(url);
    processHeaderParams(headerParams, reqBuilder);
    processCookieParams(cookieParams, 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);
    }

    // Associate callback with request (if not null) so interceptor can
    // access it when creating ProgressResponseBody
    reqBuilder.tag(callback);

    Request request = null;

    if (callback != null && reqBody != null) {
        ProgressRequestBody progressRequestBody = new ProgressRequestBody(reqBody, callback);
        request = reqBuilder.method(method, progressRequestBody).build();
    } else {
        request = reqBuilder.method(method, reqBody).build();
    }

    return request;
}
 
Example 5
Source File: ApiClient.java    From java with Apache License 2.0 4 votes vote down vote up
/**
 * Build an HTTP request 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 collectionQueryParams The collection query parameters
 * @param body The request body object
 * @param headerParams The header parameters
 * @param cookieParams The cookie parameters
 * @param formParams The form parameters
 * @param authNames The authentications to apply
 * @param callback Callback for upload/download progress
 * @return The HTTP request
 * @throws ApiException If fail to serialize the request body object
 */
public Request buildRequest(String path, String method, List<Pair> queryParams, List<Pair> collectionQueryParams, Object body, Map<String, String> headerParams, Map<String, String> cookieParams, Map<String, Object> formParams, String[] authNames, ApiCallback callback) throws ApiException {
    updateParamsForAuth(authNames, queryParams, headerParams, cookieParams);

    final String url = buildUrl(path, queryParams, collectionQueryParams);
    final Request.Builder reqBuilder = new Request.Builder().url(url);
    processHeaderParams(headerParams, reqBuilder);
    processCookieParams(cookieParams, 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);
    }

    // Associate callback with request (if not null) so interceptor can
    // access it when creating ProgressResponseBody
    reqBuilder.tag(callback);

    Request request = null;

    if (callback != null && reqBody != null) {
        ProgressRequestBody progressRequestBody = new ProgressRequestBody(reqBody, callback);
        request = reqBuilder.method(method, progressRequestBody).build();
    } else {
        request = reqBuilder.method(method, reqBody).build();
    }

    return request;
}
 
Example 6
Source File: QSMultiPartUploadRequestBody.java    From qingstor-sdk-java with Apache License 2.0 4 votes vote down vote up
@Override
public RequestBody getRequestBody(
        String contentType,
        long contentLength,
        String method,
        Map<String, Object> bodyParams,
        Map<String, Object> queryParams)
        throws QSException {
    log.debug("----QSMultiPartUploadRequestBody---");
    MediaType mediaType = MediaType.parse(contentType);
    if (bodyParams != null && bodyParams.size() > 0) {
        RequestBody requestBody = null;
        Iterator iterator = bodyParams.entrySet().iterator();

        int partNumber =
                Integer.parseInt(queryParams.get(QSConstant.PARAM_KEY_PART_NUMBER) + "");
        long offset = Long.parseLong(queryParams.get(QSConstant.PARAM_KEY_FILE_OFFSET) + "");
        while (iterator.hasNext()) {
            Map.Entry entry = (Map.Entry) iterator.next();
            String key = (String) entry.getKey();
            Object bodyObj = bodyParams.get(key);
            if (bodyObj instanceof String) {
                requestBody = RequestBody.create(mediaType, bodyObj.toString());
            } else if (bodyObj instanceof File) {
                if (contentLength == 0) contentLength = ((File) bodyObj).length();
                if (offset < 0) offset = contentLength * partNumber;
                requestBody =
                        getSeekFileRequestBody(
                                contentType, contentLength, offset, (File) bodyObj);
            } else if (bodyObj instanceof InputStream) {

                requestBody =
                        new InputStreamUploadBody(
                                contentType, (InputStream) bodyObj, contentLength, offset);

            } else {
                String jsonStr = QSStringUtil.objectToJson(key, bodyObj);
                requestBody = RequestBody.create(mediaType, jsonStr);
            }
        }
        return requestBody;
        // connection.getOutputStream().write(bodyContent.getBytes());
    } else {
        if (HttpMethod.permitsRequestBody(method)) {
            return new EmptyRequestBody(contentType);
        }
    }
    return null;
}
 
Example 7
Source File: ApiClient.java    From eve-esi with Apache License 2.0 4 votes vote down vote up
/**
 * Build an HTTP request 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 collectionQueryParams
 *            The collection query parameters
 * @param body
 *            The request body object
 * @param headerParams
 *            The header parameters
 * @param cookieParams
 *            The cookie parameters
 * @param formParams
 *            The form parameters
 * @param authNames
 *            The authentications to apply
 * @param callback
 *            Callback for upload/download progress
 * @return The HTTP request
 * @throws ApiException
 *             If fail to serialize the request body object
 */
public Request buildRequest(String path, String method, List<Pair> queryParams, List<Pair> collectionQueryParams,
        Object body, Map<String, String> headerParams, Map<String, String> cookieParams,
        Map<String, Object> formParams, String[] authNames, ApiCallback callback) throws ApiException {
    updateParamsForAuth(authNames, queryParams, headerParams, cookieParams);

    final String url = buildUrl(path, queryParams, collectionQueryParams);
    final Request.Builder reqBuilder = new Request.Builder().url(url);
    processHeaderParams(headerParams, reqBuilder);
    processCookieParams(cookieParams, 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);
    }

    // Associate callback with request (if not null) so interceptor can
    // access it when creating ProgressResponseBody
    reqBuilder.tag(callback);

    Request request = null;

    if (callback != null && reqBody != null) {
        ProgressRequestBody progressRequestBody = new ProgressRequestBody(reqBody, callback);
        request = reqBuilder.method(method, progressRequestBody).build();
    } else {
        request = reqBuilder.method(method, reqBody).build();
    }

    return request;
}