{"action":"created","issue":{"url":"https://api.github.com/repos/ipfs/go-ipfs/issues/1121","labels_url":"https://api.github.com/repos/ipfs/go-ipfs/issues/1121/labels{/name}","comments_url":"https://api.github.com/repos/ipfs/go-ipfs/issues/1121/comments","events_url":"https://api.github.com/repos/ipfs/go-ipfs/issues/1121/events","html_url":"https://github.com/ipfs/go-ipfs/issues/1121","id":70048317,"number":1121,"title":"Add command does not support json encoding","user":{"login":"travisperson","id":165274,"avatar_url":"https://avatars.githubusercontent.com/u/165274?v=3","gravatar_id":"","url":"https://api.github.com/users/travisperson","html_url":"https://github.com/travisperson","followers_url":"https://api.github.com/users/travisperson/followers","following_url":"https://api.github.com/users/travisperson/following{/other_user}","gists_url":"https://api.github.com/users/travisperson/gists{/gist_id}","starred_url":"https://api.github.com/users/travisperson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/travisperson/subscriptions","organizations_url":"https://api.github.com/users/travisperson/orgs","repos_url":"https://api.github.com/users/travisperson/repos","events_url":"https://api.github.com/users/travisperson/events{/privacy}","received_events_url":"https://api.github.com/users/travisperson/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/ipfs/go-ipfs/labels/bug","name":"bug","color":"fc2929"},{"url":"https://api.github.com/repos/ipfs/go-ipfs/labels/commands","name":"commands","color":"207de5"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2015-04-22T07:34:46Z","updated_at":"2015-05-20T19:54:53Z","closed_at":null,"body":"Currently the add command does not support JSON encoding.\r\n\r\nThis is due [to setting the output to nil](https://github.com/ipfs/go-ipfs/blob/master/core/commands/add.go#L139).\r\n\r\nA fix to this is to detect the output format and skip this `PostRun` if the encoding is anything other than `Text`.\r\n\r\n```go\r\nv, found, err := req.Option(cmds.EncShort).String()\r\nif err == nil && found && v != cmds.Text {\r\n\tlog.Debugf(\"Encoding is not text, abort and let the marshaller handle it\\n\")\r\n\treturn\r\n}\r\n```\r\n\r\nThis however introduced another issue. All output is returned as JSON. If the `previoususerProvidedEncoding` [is not set, we keep the JSON encoding](https://github.com/ipfs/go-ipfs/blob/master/commands/http/client.go#L114).\r\n\r\nThe reason we don't have a default encoding is due to using two different option keys, [in the client.go file we use](https://github.com/ipfs/go-ipfs/blob/master/commands/http/client.go#L39) `cmds.EncShort`, [while in the cli we use `encoding`](https://github.com/ipfs/go-ipfs/blob/db56c0f16a71b8cf36078a7233397de9d5b5f55a/cmd/ipfs/main.go#L239).\r\n\r\nHowever, there is another issue, since the `add` command does not use a marshaler, [the check on line 240](https://github.com/ipfs/go-ipfs/blob/db56c0f16a71b8cf36078a7233397de9d5b5f55a/cmd/ipfs/main.go#L239) will fail and switches to JSON.\r\n\r\nPostRun is used for progress, [and at some point the Marshaler was removed](https://github.com/ipfs/go-ipfs/commit/121dfb10b4b8d3a2fd4268943e65b30e5a8301a9#diff-bd0ca9f1e2d5c7ba128af01ec4c1131c).\r\n\r\nI don't know the history of this file, so there might of been a good reason (not used?). Anyways without a Marshaler the original encoding will never get set back.\r\n\r\nThis is a start of a PR I want to open\r\n\r\n```diff\r\ndiff --git a/cmd/ipfs/main.go b/cmd/ipfs/main.go\r\nindex 2ad90cf..411b833 100644\r\n--- a/cmd/ipfs/main.go\r\n+++ b/cmd/ipfs/main.go\r\n@@ -236,11 +236,11 @@ func (i *cmdInvocation) Parse(ctx context.Context, args []string) error {\r\n \r\n \t// if no encoding was specified by user, default to plaintext encoding\r\n \t// (if command doesn't support plaintext, use JSON instead)\r\n-\tif !i.req.Option(\"encoding\").Found() {\r\n+\tif !i.req.Option(cmds.EncShort).Found() {\r\n \t\tif i.req.Command().Marshalers != nil && i.req.Command().Marshalers[cmds.Text] != nil {\r\n-\t\t\ti.req.SetOption(\"encoding\", cmds.Text)\r\n+\t\t\ti.req.SetOption(cmds.EncShort, cmds.Text)\r\n \t\t} else {\r\n-\t\t\ti.req.SetOption(\"encoding\", cmds.JSON)\r\n+\t\t\ti.req.SetOption(cmds.EncShort, cmds.JSON)\r\n \t\t}\r\n \t}\r\n \r\ndiff --git a/core/commands/add.go b/core/commands/add.go\r\nindex 2b3297e..b2fc45f 100644\r\n--- a/core/commands/add.go\r\n+++ b/core/commands/add.go\r\n@@ -127,7 +127,18 @@ remains to be implemented.\r\n \t\t\t}\r\n \t\t}()\r\n \t},\r\n+\tMarshalers: cmds.MarshalerMap{\r\n+\t\tcmds.Text: func(res cmds.Response) (io.Reader, error) {\r\n+\t\t\treturn nil, nil\r\n+\t\t},\r\n+\t},\r\n \tPostRun: func(req cmds.Request, res cmds.Response) {\r\n+\t\tv, found, err := req.Option(cmds.EncShort).String()\r\n+\t\tif err == nil && found && v != cmds.Text {\r\n+\t\t\tlog.Debugf(\"Encoding is not text, abort and let the marshaller handle it\\n\")\r\n+\t\t\treturn\r\n+\t\t}\r\n+\r\n \t\tif res.Error() != nil {\r\n \t\t\treturn\r\n \t\t}\r\n```\r\n\r\nI just want to get some feedback before making any commits.\r\n\r\n/cc @jbenet @whyrusleeping @kyledrake"},"comment":{"url":"https://api.github.com/repos/ipfs/go-ipfs/issues/comments/104015561","html_url":"https://github.com/ipfs/go-ipfs/issues/1121#issuecomment-104015561","issue_url":"https://api.github.com/repos/ipfs/go-ipfs/issues/1121","id":104015561,"user":{"login":"jbenet","id":138401,"avatar_url":"https://avatars.githubusercontent.com/u/138401?v=3","gravatar_id":"","url":"https://api.github.com/users/jbenet","html_url":"https://github.com/jbenet","followers_url":"https://api.github.com/users/jbenet/followers","following_url":"https://api.github.com/users/jbenet/following{/other_user}","gists_url":"https://api.github.com/users/jbenet/gists{/gist_id}","starred_url":"https://api.github.com/users/jbenet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jbenet/subscriptions","organizations_url":"https://api.github.com/users/jbenet/orgs","repos_url":"https://api.github.com/users/jbenet/repos","events_url":"https://api.github.com/users/jbenet/events{/privacy}","received_events_url":"https://api.github.com/users/jbenet/received_events","type":"User","site_admin":false},"created_at":"2015-05-20T19:54:53Z","updated_at":"2015-05-20T19:54:53Z","body":"what's next here?"}}