Iris: Tried to pass json data but can't read the json passed using html form

Created on 19 Jul 2018  路  7Comments  路  Source: kataras/iris

How can I display all content sent to iris through

browser display after pressing submit...
{"response":"invalid character 'u' looking for beginning of value"}

how do i show all content sent to iris after i press submit?

support

Most helpful comment

Also, two tips:

  1. we have ctx.IsAjax() bool to check if it's json (it's useless on your case, but good to know)
  2. you can't use 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).
  3. you can use a typed json request/response structure for your objects, on your example you use a 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!

All 7 comments

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();

     app.Post("/up", MyHandler)

    func MyHandler(ctx iris.Context) {
            var c string 

            fmt.Printf("%v REQUEST \n",ctx.Request().Body)
            ioutil.ReadAll(ctx.Request().Body)

            if err := ctx.ReadJSON(&c); err != nil {
                    ctx.StatusCode(iris.StatusBadRequest)
                    ctx.WriteString(err.Error())
                    return
            }

            ctx.Writef("Received: %#+v\n", c)
    }

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:

  1. we have ctx.IsAjax() bool to check if it's json (it's useless on your case, but good to know)
  2. you can't use 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).
  3. you can use a typed json request/response structure for your objects, on your example you use a 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!

Was this page helpful?
0 / 5 - 0 ratings