diff --git a/connector/httpie.go b/connector/httpie.go index 7553cc1..d370abf 100644 --- a/connector/httpie.go +++ b/connector/httpie.go @@ -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) { @@ -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)) } } } @@ -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)) diff --git a/connector/httpie_test.go b/connector/httpie_test.go index 25e66af..7df46dd 100644 --- a/connector/httpie_test.go +++ b/connector/httpie_test.go @@ -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 { @@ -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) } } }