coocood/jas

Not able to read the body of the request when the Content-Type is application/json

Altonymous opened this issue · 9 comments

Because you are using Finder to read the Body of the http.Request, it makes it unavailable to Context.Request.

I don't know why you need to read the body directly, you can get the content of the body in Finder.

I haven't been able to figure out how. Assuming you can point me in the direction to do so, I wouldn't need to get it off the Context. But then again... I think it's still confusing to offer the http.Request on the Context and have it be only partial?

I want to take the Body and Unmarshal it into a struct. I'm not really interested in all the Finder methods since they don't appear to help me accomplish my task.

You can call Context.FindMap to get the json object body marshaled in map, and Context.FindSlice to get the json array body marshaled in slice.
And you can get inner parameters with Find methods and varargs.

So if I follow what you are suggesting.. you suggest I pull out the Map or Slice of the json and then convert it back into json? and then Unmarshal it from json to put it into my struct?

Because I want the json for storage and I want the data in my struct for doing calculations. Seems like a lot of overhead when all I want is the json so I can Unmarshall it to my object?

I'm not trying to be obtuse or rude, I'm just trying to understand.

OK, I understand your need, I should think of good way to make the json body unmarshaled to a struct.

I think that would be great! Please make the json available directly, that's all I need. I use redis and store the data in json format. I can easily Unmarshal it myself. It's only a couple lines of code.

It is easy to just make a config option to disable auto unmarshal behavior, but I'm trying to find a better solution without the need to config it.

Yeah that's the first thing that came to mind for me too. I didn't want to submit a pull request for just that though. I've been thinking about it myself but need to head to bed. If you haven't come up with anything by morning I'll go back to thinking on it.

It might be helpful for performance reasons to have a disableFinder config though.

added an Config option and two new methods for Context.