A simple json request returns a strange string rather than json itself
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I'm trying to return a simple json in Go. This is a web app and here's a part of a handler:
func JsonTest1(w http.ResponseWriter, r *http.Request) {
test1 := "something1"
test2 := 456
j1 := byte(fmt.Sprintf(`
{
data: {
"test1": %s,
"test2": %d
}
}
`, test1, test2))
j2, _ := json.Marshal(&j1)
w.Header().Set("Content-Type", "application/json")
w.Write(j2)
}
When I'm doing a request via curl, I receive something like:
CiAgICB7CiAgICAgIGRhdGE6IHsKICAgICAgICAicmVkaXJlY3RfdXJsIjogdGVzdF9yZWRpcl91cmwxLAogICAgICAgICJtZXNzYWdlIjogdGVzdCBtc2cgMQogICAgICB9CiAgICB9CiAg
Why? How to fix that?
json go
add a comment |
I'm trying to return a simple json in Go. This is a web app and here's a part of a handler:
func JsonTest1(w http.ResponseWriter, r *http.Request) {
test1 := "something1"
test2 := 456
j1 := byte(fmt.Sprintf(`
{
data: {
"test1": %s,
"test2": %d
}
}
`, test1, test2))
j2, _ := json.Marshal(&j1)
w.Header().Set("Content-Type", "application/json")
w.Write(j2)
}
When I'm doing a request via curl, I receive something like:
CiAgICB7CiAgICAgIGRhdGE6IHsKICAgICAgICAicmVkaXJlY3RfdXJsIjogdGVzdF9yZWRpcl91cmwxLAogICAgICAgICJtZXNzYWdlIjogdGVzdCBtc2cgMQogICAgICB9CiAgICB9CiAg
Why? How to fix that?
json go
2
It's a base64 encoded string: { data: { "redirect_url": test_redir_url1, "message": test msg 1 } }
– Kurohige
Jan 3 at 15:31
@Kurohige - my task is to return json
– Pappani
Jan 3 at 15:33
add a comment |
I'm trying to return a simple json in Go. This is a web app and here's a part of a handler:
func JsonTest1(w http.ResponseWriter, r *http.Request) {
test1 := "something1"
test2 := 456
j1 := byte(fmt.Sprintf(`
{
data: {
"test1": %s,
"test2": %d
}
}
`, test1, test2))
j2, _ := json.Marshal(&j1)
w.Header().Set("Content-Type", "application/json")
w.Write(j2)
}
When I'm doing a request via curl, I receive something like:
CiAgICB7CiAgICAgIGRhdGE6IHsKICAgICAgICAicmVkaXJlY3RfdXJsIjogdGVzdF9yZWRpcl91cmwxLAogICAgICAgICJtZXNzYWdlIjogdGVzdCBtc2cgMQogICAgICB9CiAgICB9CiAg
Why? How to fix that?
json go
I'm trying to return a simple json in Go. This is a web app and here's a part of a handler:
func JsonTest1(w http.ResponseWriter, r *http.Request) {
test1 := "something1"
test2 := 456
j1 := byte(fmt.Sprintf(`
{
data: {
"test1": %s,
"test2": %d
}
}
`, test1, test2))
j2, _ := json.Marshal(&j1)
w.Header().Set("Content-Type", "application/json")
w.Write(j2)
}
When I'm doing a request via curl, I receive something like:
CiAgICB7CiAgICAgIGRhdGE6IHsKICAgICAgICAicmVkaXJlY3RfdXJsIjogdGVzdF9yZWRpcl91cmwxLAogICAgICAgICJtZXNzYWdlIjogdGVzdCBtc2cgMQogICAgICB9CiAgICB9CiAg
Why? How to fix that?
json go
json go
asked Jan 3 at 15:26
PappaniPappani
72
72
2
It's a base64 encoded string: { data: { "redirect_url": test_redir_url1, "message": test msg 1 } }
– Kurohige
Jan 3 at 15:31
@Kurohige - my task is to return json
– Pappani
Jan 3 at 15:33
add a comment |
2
It's a base64 encoded string: { data: { "redirect_url": test_redir_url1, "message": test msg 1 } }
– Kurohige
Jan 3 at 15:31
@Kurohige - my task is to return json
– Pappani
Jan 3 at 15:33
2
2
It's a base64 encoded string: { data: { "redirect_url": test_redir_url1, "message": test msg 1 } }
– Kurohige
Jan 3 at 15:31
It's a base64 encoded string: { data: { "redirect_url": test_redir_url1, "message": test msg 1 } }
– Kurohige
Jan 3 at 15:31
@Kurohige - my task is to return json
– Pappani
Jan 3 at 15:33
@Kurohige - my task is to return json
– Pappani
Jan 3 at 15:33
add a comment |
1 Answer
1
active
oldest
votes
When you JSON-encode a byte
, it will be rendered as a base64-encoded string, the most effective way to represent an arbitrary byte slice/array in JSON (the only real alternative being "field": [7, 129, 13, 48, ...]
and so on). In your code, however, you're doing a couple of unusual things that may not be what's intended:
- You're manually creating a JSON-ish string using
Sprintf
, then trying to JSON-encode your JSON.json.Marshal
is for taking some arbitrary Go value and rendering it as JSON. - Your manually-created JSON is invalid; you have a string field value that's unquoted.
What you want is probably one of these options:
// Manually-created *valid* JSON
func JsonTest1(w http.ResponseWriter, r *http.Request) {
test1 := "something1"
test2 := 456
// %q instead of %s gives us a quoted string:
j1 := byte(fmt.Sprintf(`
{
data: {
"test1": %q,
"test2": %d
}
}
`, test1, test2))
w.Header().Set("Content-Type", "application/json")
w.Write(j1)
}
// JSON created with json.Marshal
func JsonTest2(w http.ResponseWriter, r *http.Request) {
test1 := "something1"
test2 := 456
data := map[string]interface{}{
"data": map[string]interface{}{
"test1": test1,
"test2": test2,
},
}
j1, _ := json.Marshal(data)
w.Header().Set("Content-Type", "application/json")
w.Write(j1)
}
show me how to fix that
– Pappani
Jan 3 at 15:38
1
I did, two different ways.
– Adrian
Jan 3 at 15:38
I would note that usingfmt.Sprintf()
is not advisable: it doesn't know about JSON syntax and it's easy to generate invalid JSON that way (especially if string values are substituted in).
– icza
Jan 3 at 15:46
Definitely agree but to each their own.Sprintf
is a legitimate way to generate JSON, but not one I would choose in 99% of situations.
– Adrian
Jan 3 at 16:03
yes, the json is invalid: "Unexpected token d in JSON at position 13"
– Pappani
Jan 3 at 18:41
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54025216%2fa-simple-json-request-returns-a-strange-string-rather-than-json-itself%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
When you JSON-encode a byte
, it will be rendered as a base64-encoded string, the most effective way to represent an arbitrary byte slice/array in JSON (the only real alternative being "field": [7, 129, 13, 48, ...]
and so on). In your code, however, you're doing a couple of unusual things that may not be what's intended:
- You're manually creating a JSON-ish string using
Sprintf
, then trying to JSON-encode your JSON.json.Marshal
is for taking some arbitrary Go value and rendering it as JSON. - Your manually-created JSON is invalid; you have a string field value that's unquoted.
What you want is probably one of these options:
// Manually-created *valid* JSON
func JsonTest1(w http.ResponseWriter, r *http.Request) {
test1 := "something1"
test2 := 456
// %q instead of %s gives us a quoted string:
j1 := byte(fmt.Sprintf(`
{
data: {
"test1": %q,
"test2": %d
}
}
`, test1, test2))
w.Header().Set("Content-Type", "application/json")
w.Write(j1)
}
// JSON created with json.Marshal
func JsonTest2(w http.ResponseWriter, r *http.Request) {
test1 := "something1"
test2 := 456
data := map[string]interface{}{
"data": map[string]interface{}{
"test1": test1,
"test2": test2,
},
}
j1, _ := json.Marshal(data)
w.Header().Set("Content-Type", "application/json")
w.Write(j1)
}
show me how to fix that
– Pappani
Jan 3 at 15:38
1
I did, two different ways.
– Adrian
Jan 3 at 15:38
I would note that usingfmt.Sprintf()
is not advisable: it doesn't know about JSON syntax and it's easy to generate invalid JSON that way (especially if string values are substituted in).
– icza
Jan 3 at 15:46
Definitely agree but to each their own.Sprintf
is a legitimate way to generate JSON, but not one I would choose in 99% of situations.
– Adrian
Jan 3 at 16:03
yes, the json is invalid: "Unexpected token d in JSON at position 13"
– Pappani
Jan 3 at 18:41
|
show 1 more comment
When you JSON-encode a byte
, it will be rendered as a base64-encoded string, the most effective way to represent an arbitrary byte slice/array in JSON (the only real alternative being "field": [7, 129, 13, 48, ...]
and so on). In your code, however, you're doing a couple of unusual things that may not be what's intended:
- You're manually creating a JSON-ish string using
Sprintf
, then trying to JSON-encode your JSON.json.Marshal
is for taking some arbitrary Go value and rendering it as JSON. - Your manually-created JSON is invalid; you have a string field value that's unquoted.
What you want is probably one of these options:
// Manually-created *valid* JSON
func JsonTest1(w http.ResponseWriter, r *http.Request) {
test1 := "something1"
test2 := 456
// %q instead of %s gives us a quoted string:
j1 := byte(fmt.Sprintf(`
{
data: {
"test1": %q,
"test2": %d
}
}
`, test1, test2))
w.Header().Set("Content-Type", "application/json")
w.Write(j1)
}
// JSON created with json.Marshal
func JsonTest2(w http.ResponseWriter, r *http.Request) {
test1 := "something1"
test2 := 456
data := map[string]interface{}{
"data": map[string]interface{}{
"test1": test1,
"test2": test2,
},
}
j1, _ := json.Marshal(data)
w.Header().Set("Content-Type", "application/json")
w.Write(j1)
}
show me how to fix that
– Pappani
Jan 3 at 15:38
1
I did, two different ways.
– Adrian
Jan 3 at 15:38
I would note that usingfmt.Sprintf()
is not advisable: it doesn't know about JSON syntax and it's easy to generate invalid JSON that way (especially if string values are substituted in).
– icza
Jan 3 at 15:46
Definitely agree but to each their own.Sprintf
is a legitimate way to generate JSON, but not one I would choose in 99% of situations.
– Adrian
Jan 3 at 16:03
yes, the json is invalid: "Unexpected token d in JSON at position 13"
– Pappani
Jan 3 at 18:41
|
show 1 more comment
When you JSON-encode a byte
, it will be rendered as a base64-encoded string, the most effective way to represent an arbitrary byte slice/array in JSON (the only real alternative being "field": [7, 129, 13, 48, ...]
and so on). In your code, however, you're doing a couple of unusual things that may not be what's intended:
- You're manually creating a JSON-ish string using
Sprintf
, then trying to JSON-encode your JSON.json.Marshal
is for taking some arbitrary Go value and rendering it as JSON. - Your manually-created JSON is invalid; you have a string field value that's unquoted.
What you want is probably one of these options:
// Manually-created *valid* JSON
func JsonTest1(w http.ResponseWriter, r *http.Request) {
test1 := "something1"
test2 := 456
// %q instead of %s gives us a quoted string:
j1 := byte(fmt.Sprintf(`
{
data: {
"test1": %q,
"test2": %d
}
}
`, test1, test2))
w.Header().Set("Content-Type", "application/json")
w.Write(j1)
}
// JSON created with json.Marshal
func JsonTest2(w http.ResponseWriter, r *http.Request) {
test1 := "something1"
test2 := 456
data := map[string]interface{}{
"data": map[string]interface{}{
"test1": test1,
"test2": test2,
},
}
j1, _ := json.Marshal(data)
w.Header().Set("Content-Type", "application/json")
w.Write(j1)
}
When you JSON-encode a byte
, it will be rendered as a base64-encoded string, the most effective way to represent an arbitrary byte slice/array in JSON (the only real alternative being "field": [7, 129, 13, 48, ...]
and so on). In your code, however, you're doing a couple of unusual things that may not be what's intended:
- You're manually creating a JSON-ish string using
Sprintf
, then trying to JSON-encode your JSON.json.Marshal
is for taking some arbitrary Go value and rendering it as JSON. - Your manually-created JSON is invalid; you have a string field value that's unquoted.
What you want is probably one of these options:
// Manually-created *valid* JSON
func JsonTest1(w http.ResponseWriter, r *http.Request) {
test1 := "something1"
test2 := 456
// %q instead of %s gives us a quoted string:
j1 := byte(fmt.Sprintf(`
{
data: {
"test1": %q,
"test2": %d
}
}
`, test1, test2))
w.Header().Set("Content-Type", "application/json")
w.Write(j1)
}
// JSON created with json.Marshal
func JsonTest2(w http.ResponseWriter, r *http.Request) {
test1 := "something1"
test2 := 456
data := map[string]interface{}{
"data": map[string]interface{}{
"test1": test1,
"test2": test2,
},
}
j1, _ := json.Marshal(data)
w.Header().Set("Content-Type", "application/json")
w.Write(j1)
}
answered Jan 3 at 15:34
AdrianAdrian
19.7k44051
19.7k44051
show me how to fix that
– Pappani
Jan 3 at 15:38
1
I did, two different ways.
– Adrian
Jan 3 at 15:38
I would note that usingfmt.Sprintf()
is not advisable: it doesn't know about JSON syntax and it's easy to generate invalid JSON that way (especially if string values are substituted in).
– icza
Jan 3 at 15:46
Definitely agree but to each their own.Sprintf
is a legitimate way to generate JSON, but not one I would choose in 99% of situations.
– Adrian
Jan 3 at 16:03
yes, the json is invalid: "Unexpected token d in JSON at position 13"
– Pappani
Jan 3 at 18:41
|
show 1 more comment
show me how to fix that
– Pappani
Jan 3 at 15:38
1
I did, two different ways.
– Adrian
Jan 3 at 15:38
I would note that usingfmt.Sprintf()
is not advisable: it doesn't know about JSON syntax and it's easy to generate invalid JSON that way (especially if string values are substituted in).
– icza
Jan 3 at 15:46
Definitely agree but to each their own.Sprintf
is a legitimate way to generate JSON, but not one I would choose in 99% of situations.
– Adrian
Jan 3 at 16:03
yes, the json is invalid: "Unexpected token d in JSON at position 13"
– Pappani
Jan 3 at 18:41
show me how to fix that
– Pappani
Jan 3 at 15:38
show me how to fix that
– Pappani
Jan 3 at 15:38
1
1
I did, two different ways.
– Adrian
Jan 3 at 15:38
I did, two different ways.
– Adrian
Jan 3 at 15:38
I would note that using
fmt.Sprintf()
is not advisable: it doesn't know about JSON syntax and it's easy to generate invalid JSON that way (especially if string values are substituted in).– icza
Jan 3 at 15:46
I would note that using
fmt.Sprintf()
is not advisable: it doesn't know about JSON syntax and it's easy to generate invalid JSON that way (especially if string values are substituted in).– icza
Jan 3 at 15:46
Definitely agree but to each their own.
Sprintf
is a legitimate way to generate JSON, but not one I would choose in 99% of situations.– Adrian
Jan 3 at 16:03
Definitely agree but to each their own.
Sprintf
is a legitimate way to generate JSON, but not one I would choose in 99% of situations.– Adrian
Jan 3 at 16:03
yes, the json is invalid: "Unexpected token d in JSON at position 13"
– Pappani
Jan 3 at 18:41
yes, the json is invalid: "Unexpected token d in JSON at position 13"
– Pappani
Jan 3 at 18:41
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54025216%2fa-simple-json-request-returns-a-strange-string-rather-than-json-itself%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
It's a base64 encoded string: { data: { "redirect_url": test_redir_url1, "message": test msg 1 } }
– Kurohige
Jan 3 at 15:31
@Kurohige - my task is to return json
– Pappani
Jan 3 at 15:33