GoogleCloudPlatform / go-endpoints

Cloud Endpoints for Go
https://go-endpoints.appspot.com
Apache License 2.0
255 stars 56 forks source link

Request body is always empty #71

Closed robskie closed 9 years ago

robskie commented 9 years ago

Hi!

I'm trying to add some extra field in the request which are not part of the message request definition like what bossylobster suggested here, but I'm always getting an empty request body. Apparently, the request body is read but not restored to its original content when passed to service methods.

EDIT: Another way of adding "hidden" fields would be to add another endpoints tag "-" which will allow tagged fields to be ignored by endpoints and yet still be unpacked if it is added in the request.

miko-code commented 9 years ago

can you elaborate and upload your code ?

robskie commented 9 years ago

So for example you have the following service method:

func (s *TestService) Test(req *http.Request, r *RequestMessage) error {
    body, _ := ioutil.ReadAll(req.Body)
    // Do something with body
    return nil
}

In this case, body will always be an empty slice regardless of the sent request message. I expect that if the request message is non-empty, body would also be non-empty.

As for the suggestion that another endpoints tag, endpoints:"-", be added for hidden fields, let's say you have the following request message:

type RequestMessage struct {
    Field1 int `json:"field1"`
    Field2 int `json:"field2"`
    Hidden int `json:"hidden"`
}

And you want to have the Hidden field to be ignored by endpoints but still be able to retrieve its value in service methods. One possible solution would be to change its tag to json:"-", and just extract its value from the raw request body.

Another option would be to leave its json tag unchanged and just add another endpoints tag, endpoints:"-", which is more convenient since you wouldn't need to manually extract the Hidden field's value from the request body. This is done automatically by the json unmarshaller if the sent request has it.