# ok2curl **Repository Path**: openneusoft/ok2curl ## Basic Information - **Project Name**: ok2curl - **Description**: 将OkHttp请求转换为curl日志 - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-04-17 - **Last Updated**: 2024-07-04 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Ok2Curl Convert OkHttp requests into curl logs. - Original project website: https://github.com/mrmike/Ok2Curl - Original project baseline version: v0.7.0, sha1:2d915240b07975fab214a85e24fbb9aaf47ed840 ## Usage Add library to project dependencies. Library is hosted on Maven. ``` allprojects{ repositories{ mavenCentral() } } implementation 'io.github.dzsf:ok2curl:1.0.0' ``` To start logging requests with Ok2Curl add interceptor to OkHttp client. ```java OkHttpClient okHttp = new OkHttpClient.Builder() .addInterceptor(new CurlInterceptor(new Loggable() { public static final String LOG_TAG = "Ok2Curl"; public static final HiLogLabel LOG_LABEL = new HiLogLabel(HiLog.LOG_APP,0x00101, LOG_TAG); @Override public void log(String message) { HiLog.debug(LOG_LABEL, message); } })) .build(); ``` ## Result With Ok2Curl set up correctly every executed request will be transformed into curl log ## Network interceptors By default Ok2Curl uses application interceptors from OkHttp which is adequate for most cases. But sometimes you may want to use network interceptor e.g. to log Cookies set via [CookieHandler](http://docs.oracle.com/javase/6/docs/api/java/net/CookieHandler.html). In such a case add interceptor the same way as below: ``` OkHttpClient okHttp = new OkHttpClient.Builder() .addNetworkInterceptor(new CurlInterceptor()) .build(); ``` ## Header modifiers Ok2Curl allows you to modify any header before creating curl command. and add this modifier to CurlInterceptor. ``` final BasicAuthorizationHeaderModifier modifier = new BasicAuthorizationHeaderModifier(new Base64Decoder()); final List modifiers = Collections.singletonList(modifier); final CurlInterceptor curlInterceptor = new CurlInterceptor(new HosLogger(), modifiers); ``` ## Options Ok2Curl supports basic Curl options. In order to use options use the following code: ``` final Options options = Options.builder() .insecure() .connectTimeout(120) .retry(5) .build(); final CurlInterceptor interceptor = new CurlInterceptor(logger, options); ``` Since now every Curl command will start with `curl --insecure --connect-timeout 120 --retry 5...` ### Supported options * --insecure * --max-time seconds * --connect-timeout seconds * --retry num * --compressed * --location If would like to support any new options please feel free to open PR. Full list of curl options is available [here](https://curl.haxx.se/docs/manpage.html). ## Versions - v1.0.0 ## License Copyright 2018 Michał Moczulski Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.