Skip to content

feat(enhancement): Use http.Cookie to compose values in generated curl commands - #1250

Merged
jeevatkm merged 1 commit into
go-resty:v3from
Aurantium-47:codex/fix-curl-cookie-serialization
Nov 7, 2026
Merged

jeevatkm merged 1 commit into
go-resty:v3from
Aurantium-47:codex/fix-curl-cookie-serialization

Conversation

@Aurantium-47

Copy link
Copy Markdown
Contributor

Cookie-jar values are URL-query-escaped when generating curl commands. For example, a request sends session=YWJj+/==, but its generated command sends session=YWJj%2B%2F%3D%3D. Percent signs and quoted values are also changed, so copying the command cannot reproduce the original request.

Build the cookie header with http.Request.AddCookie, matching the serialization used by the HTTP client. Regression tests compare generated commands with headers received by a loopback server for base64, percent signs, spaces, commas, explicitly quoted values, and plain/empty controls.

Validated on Go 1.27.2 / Windows: all curl tests, go vet, go build, formatting, 100% coverage of the changed helper, and 18 real curl replays. The full shuffled suite has one unrelated failure in the existing invalid-request trace timing assertion, also reproduced on the unchanged base. Race tests were not run locally.

Prepared and tested with OpenAI Codex assistance.

Assisted-by: OpenAI GPT-6 <noreply@openai.com>
@jeevatkm

Copy link
Copy Markdown
Member

@Aurantium-47 I need time to analyze and get back. If you have any handy example to demonstrate the issue, kindly share.

@Aurantium-47

Aurantium-47 commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor Author

Here is a runnable example using only Resty and a loopback HTTP server:

package main

import (
    "fmt"
    "io"
    "net/http"
    "net/http/cookiejar"
    "net/http/httptest"
    "net/url"

    "resty.dev/v3"
)

func main() {
    server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        _, _ = io.WriteString(w, r.Header.Get("Cookie"))
    }))
    defer server.Close()
    u, _ := url.Parse(server.URL)
    jar, _ := cookiejar.New(nil)
    jar.SetCookies(u, []*http.Cookie{{Name: "session", Value: "YWJj+/=="}})
    client := resty.New().SetCookieJar(jar)
    defer client.Close()
    request := client.R().SetMethod(http.MethodGet).SetURL(server.URL).SetCurlCmdGenerate(true)
    fmt.Println("Generated command:", request.CurlCmd())
    response, err := request.Get(server.URL)
    if err != nil {
        panic(err)
    }
    fmt.Println("Cookie received by server:", response.String())
}

On the original PR base (4e64897f), the generated header is Cookie: session=YWJj%2B%2F%3D%3D, while the server receives session=YWJj+/==. With this PR (eba3a0e2), both contain session=YWJj+/==.

OpenAI Codex ran this on Windows with Go 1.27.2 against the PR checkout, then with a Go overlay replacing only curl.go with the original base version. Both runs exited successfully. This example compares the generated command with the actual HTTP request; it does not execute the curl command. No external service or real session cookie is needed.

Prepared and tested with OpenAI Codex assistance.

@codecov

codecov Bot commented Nov 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.75%. Comparing base (4e64897) to head (eba3a0e).
⚠️ Report is 10 commits behind head on v3.

Additional details and impacted files
@@            Coverage Diff             @@
##               v3    #1250      +/-   ##
==========================================
- Coverage   99.75%   99.75%   -0.01%     
==========================================
  Files          20       20              
  Lines        3631     3628       -3     
==========================================
- Hits         3622     3619       -3     
  Misses          9        9              
Flag Coverage Δ
unittests 99.75% <100.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jeevatkm jeevatkm changed the title fix: preserve cookie values in generated curl commands feat(enhancement): Use http.Cookie to compose values in generated curl commands Nov 7, 2026
@jeevatkm jeevatkm added enhancement v3 For resty v3 labels Nov 7, 2026
@jeevatkm
jeevatkm merged commit 7994f03 into go-resty:v3 Nov 7, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement v3 For resty v3

Development

Successfully merging this pull request may close these issues.

2 participants