moul/http2curl

Doesn't seem to always quote body data

Closed this issue · 2 comments

quii commented

Here's a failing test

func TestItQuotesFormData(t *testing.T){
    form := url.Values{}
    form.Add("age", "10")
    form.Add("name", "Hudson")
    body := form.Encode()

    req, _ := http.NewRequest(http.MethodPost, "http://foo.com/cats", ioutil.NopCloser(bytes.NewBufferString(body)))
    req.Header.Set("API_KEY", "123")

    command, _ := GetCurlCommand(req)

    expected := `curl -X POST -d "age=10&name=Hudson" -H "API_KEY: 123" 'http://foo.com/cats'`

    if command.String() != expected{
        t.Error("Didnt work, got")
        t.Log(command.String())
        t.Log("expected")
        t.Log(expected)
    }

}

Gets

    http2curl_test.go:56: Didnt work, got
    http2curl_test.go:57: curl -X POST -d age=10&name=Hudson -H "API_KEY: 123" 'http://foo.com/cats'
    http2curl_test.go:58: expected
    http2curl_test.go:59: curl -X POST -d "age=10&name=Hudson" -H "API_KEY: 123" 'http://foo.com/cats'

The resulting curl is not valid because the form data needs to be quoted. I tried playing with your algorithm for quoting but ended up breaking lots of stuff. Will give it a go later unless you can see an easy fix?

I have a pull request that fixes this #5

moul commented

Fixed with #5, thanks again @drewlesueur