Skip to content

Commit

Permalink
fix a bug on httpie to curl
Browse files Browse the repository at this point in the history
  • Loading branch information
dcb9 committed Dec 21, 2022
1 parent f52aa63 commit ee09edb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
14 changes: 9 additions & 5 deletions connector/httpie.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package connector

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"

"github.com/dcb9/curl2httpie/curl"
"github.com/dcb9/curl2httpie/httpie"
curlTransformer "github.com/dcb9/curl2httpie/transformers/curl"
"io/ioutil"
"os"
"strings"
)

func Httpie2Curl(args []string) (cmdStringer fmt.Stringer, warningMessages []WarningMessage, err error) {
Expand Down Expand Up @@ -89,7 +90,7 @@ func Httpie2Curl(args []string) (cmdStringer fmt.Stringer, warningMessages []War
}
data[i.K] = bytes
} else {
data[i.K] = i.V
data[i.K] = json.RawMessage([]byte(i.V))
}
}
}
Expand All @@ -100,7 +101,10 @@ func Httpie2Curl(args []string) (cmdStringer fmt.Stringer, warningMessages []War
if hasData {
if isJSONContentType {
var bs []byte
// FIXME
bs, err = json.Marshal(data)
if err != nil {
return nil, nil, err
}
curlCmdLine.Options = append(curlCmdLine.Options, curl.NewJSONHeader(), curl.NewData(string(bs)))
} else {
fields := make([]string, 0, len(data))
Expand Down
12 changes: 11 additions & 1 deletion connector/httpie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ func TestHttpie2Curl(t *testing.T) {
},
}

// cases = []struct {
// in []string
// want string
// }{
// {
// []string{"http", "--auth", "username", "example.org", "id==1", "foo:bar", "foo=bar", `a:={"foo": "bar"}`},
// `curl --user 'username' --header 'foo: bar' --header 'Content-Type: application/json' --data '{"a":{"foo":"bar"},"foo":"bar"}' 'example.org?id=1'`,
// },
// }

for _, c := range cases {
gotStringer, warningMessages, err := Httpie2Curl(c.in[1:])
if len(warningMessages) > 0 {
Expand All @@ -101,7 +111,7 @@ func TestHttpie2Curl(t *testing.T) {

want := c.want
if got := gotStringer.String(); got != want {
t.Errorf("Httpie2Curl error got: %s\n\twant: %s\n\tin: %#v", got, want, c.in)
t.Errorf("Httpie2Curl error\ngot:\n%s\nwant:\n%s\nin:\n%#v", got, want, c.in)
}
}
}

0 comments on commit ee09edb

Please sign in to comment.