Repository: square/okhttp
File: okhttp/src/commonJvmAndroid/kotlin/okhttp3/Request.kt (Kotlin)

Code before:

        val contentEncoding = headers["Content-Encoding"]
        check(contentEncoding == null) {
          "Content-Encoding already set: $contentEncoding"
        }

        headers.add("Content-Encoding", "gzip")
        body = GzipRequestBody(identityBody)
      }

    open fun build(): Request = Request(this)
  }

  /**
   * Returns a cURL command equivalent to this request, useful for debugging and reproducing requests.
   *
   * This includes the HTTP method, headers, request body (if present), and URL.
   *
   * Example:
   * ```
   * curl -X POST -H "Authorization: Bearer token" --data "{\"key\":\"value\"}" "https://example.com/api"
   * ```
   *
   * @return a cURL command string representing this request.
   */
  fun curl(): String {
    val curl = StringBuilder("curl")
    // Add method if not GET
    if (method != "GET") {
      curl.append(" -X ").append(method)
    }

    for ((name, value) in headers) {
      curl
        .append(" -H ")
        .append("\"")
        .append(name)
        .append(": ")
        .append(value)
        .append("\"")
    }

    body?.let { requestBody ->
      val buffer = okio.Buffer()
      requestBody.writeTo(buffer)

      val contentType = requestBody.contentType()?.toString() ?: ""

      if (contentType.startsWith("text/") ||
        contentType.contains("json") ||
        contentType.contains("xml") ||

Review comment (suggestion):
This should probably be named `toCurl`, since it's not a property of the request itself.

Code after:
     *
     * @throws IllegalStateException if this request doesn't have a request body, or if it already
     *     has a 'Content-Encoding' header.
     */
    fun gzip() =
      apply {
        val identityBody =
          body
            ?: throw IllegalStateException("cannot gzip a request that has no body")

        val contentEncoding = headers["Content-Encoding"]
        check(contentEncoding == null) {
          "Content-Encoding already set: $contentEncoding"
        }

        headers.add("Content-Encoding", "gzip")
        body = GzipRequestBody(identityBody)
      }

    open fun build(): Request = Request(this)
  }

  /**
   * Returns a cURL command equivalent to this request, useful for debugging and reproducing requests.
   *
   * This includes the HTTP method, headers, request body (if present), and URL.
   *
   * Example:
   * ```
   * curl -X POST -H "Authorization: Bearer token" --data "{\"key\":\"value\"}" "https://example.com/api"
   * ```
   *
   * **Note:** This method will write the body
   * to a temporary [okio.Buffer] in memory. This may have side effects if the [RequestBody] is streaming
   * or can be consumed only once. Calling this method might prevent re-sending the request body later.
   *
   * @param binaryFileName default file name to use when dumping binary body data to a file (default: `"request_body.bin"`)
   * @param binaryMode default mode to use when writing binary body data (default: `"BinaryMode.STDIN"`)
   * @return a cURL command string representing this request.
   */
  @JvmOverloads
  fun toCurl(
    binaryMode: BinaryMode = BinaryMode.STDIN,
    binaryFileName: String? = "request_body.bin",
  ): String {
    val curl = StringBuilder("curl")

    // Add method if not GET
    if (method != "GET") {
      curl.append(" -X ").append(method)
    }