I take a look on Angular API for $resource and I didn't find some way to send a Request Body
to a RESTful service.
I know this is possible using $http approach, like here, so, is it also possible to do using $resource
?
Apparently this is the options for $resource
.
action – {string} – The name of action. This name becomes the name of the method on your resource object.
method – {string} – HTTP request method. Valid methods are: GET, POST, PUT, DELETE, and JSONP
params – {object=} – Optional set of pre-bound parameters for this action.
isArray – {boolean=} – If true then the returned object for this action is an array, see returns section.
At the moment I didn't found any way to send a request payload containing an JSON object.
So, yes, you can send a body with GET, and no, it is never useful to do so. This is part of the layered design of HTTP/1.1 that will become clear again once the spec is partitioned (work in progress). Yes, you can send a request body with GET but it should not have any meaning.
Essentially, the Content-Type header specifies a MIME type telling the server what kind of data is in the request body. For example, using the application/json MIME type tells the server that the request body is a valid JSON object. For a list of commonly used MIME types, we can refer to the MDN documentation.
GET requests don't have a request body, so all parameters must appear in the URL or in a header. While the HTTP standard doesn't define a limit for how long URLs or headers can be, mostHTTP clients and servers have a practical limit somewhere between 2 kB and 8 kB.
A request body is data sent by the client to your API. A response body is the data your API sends to the client. Your API almost always has to send a response body. But clients don't necessarily need to send request bodies all the time.
Building on @gargc
's answer, you can pass parameters and a body to a resource's method:
myResource.save({ param: myParam }, myObject);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With