How can I display all content sent to iris through
Pass JSON to client: ctx.JSON(v), example: https://github.com/kataras/iris/blob/master/_examples/http_responsewriter/write-rest/main.go#L61
Read JSON from client: ctx.ReadJSON, example: https://github.com/kataras/iris/blob/master/_examples/http_request/read-json/main.go
Thanks for the reply. Appreciate this. But I've passed the fields just that they are not showing. How can I debug this situation? I would like to see all information (raw data) passed to it.
By fields you mean html form? If so then check this: https://github.com/kataras/iris/tree/master/_examples/http_request/read-form
About debugging, quite easy, you can read the whole request sent to the server by the client by reading the ctx.Request().Body, the easiest way to do it is by: ioutil.ReadAll(ctx.Request().Body)
this is what i did. iris detected the input and POST to /up works but i can't get any information out of it except
&{0xc420564000 <nil> <nil> false true {0 0} false false false 0x6ddc50} REQUEST
which is totally irrelevant to what i have for the json at all. my browser CTRL+SHIFT+J shows json is sent though.
....
url : "/up",
type: "POST",
data : jsonData,
success:function(data, textStatus, jqXHR)
{
$("#response").text("");
$("#response").append(data);
$("#response").slideDown("slow");
}
});
$("#msg").show("slow");
e.preventDefault();
this is the full html code. u can try to understand what i mean. it's detecting the json POST but not showing anything though chrome does show it sent.
https://pastebin.com/Tn3AiHtJ
Man, this is essential web knowedge:
If you want to send the data as JSON, you have to encode it first:
data: JSON.stringify(jsonData),
jQuery does not convert objects or arrays to JSON automatically.
Also, two tips:
ctx.IsAjax() bool to check if it's json (it's useless on your case, but good to know)ioutil.ReadAll on a reader and after ctx.ReadXXX because the body is consumed by the ioutil.ReadAll (basic-to-medium level Go knowedge), this is why we have a feature specific for this case, the WithoutBodyConsumptionOnUnmarshal: app.Run(iris.Addr(":8080"), iris.WithoutBodyConsumptionOnUnmarshal) so you can ctx.ReadJSON first and after ioutil.ReadAll. How it works internally? I did this using a no operation closer: ctx.request.Body = ioutil.NopCloser(bytes.NewBuffer(rawData)) where rawData, err := ioutil.ReadAll(ctx.request.Body).string (maybe just for the shake of the example?) but you can do that as well:type myjson struct {
X string `json:"X"`
U string `json:"U"`
N string `json:"N"`
E string `json:"E"`
S string `json:"S"`
}
Glad that I could help @unisqu , if you need anything else please do not hesitate to ask!
Most helpful comment
Also, two tips:
ctx.IsAjax() boolto check if it's json (it's useless on your case, but good to know)ioutil.ReadAllon a reader and afterctx.ReadXXXbecause the body is consumed by theioutil.ReadAll(basic-to-medium level Go knowedge), this is why we have a feature specific for this case, theWithoutBodyConsumptionOnUnmarshal:app.Run(iris.Addr(":8080"), iris.WithoutBodyConsumptionOnUnmarshal)so you canctx.ReadJSONfirst and afterioutil.ReadAll. How it works internally? I did this using ano operation closer:ctx.request.Body = ioutil.NopCloser(bytes.NewBuffer(rawData))whererawData, err := ioutil.ReadAll(ctx.request.Body).string(maybe just for the shake of the example?) but you can do that as well:Glad that I could help @unisqu , if you need anything else please do not hesitate to ask!