axios: GET request does not send data (JSON).
Code:
axios.get("/api/v1/post/allFromUser", {data: {"author": "fharding"}}).then(response => {
this.posts = response.data;
}).catch(err => {
alert(err);
console.log(err);
})
When I switch this to POST the data get’s send just find, but there is no body when sent with GET. Sending JSON with GET is not against spec, why is this not working? I’ve also tried using json.stringify for sending the data.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 60
- Comments: 55 (4 by maintainers)
because get requests doesn’t have a body use query params instead
I think GET is not for sending data. the only way to pass information with GET is in through request parameter.
There is nothing in the spec that says it can’t send a body.
On Mar 24, 2017 5:47 AM, “amgadserry” notifications@github.com wrote:
by the way , it works when I used params like this . axios({ method: “get”, url: “http://localhost:5000/v1/data/users/”, params: { username: this.state.username, password: this.state.password } }).then(res => console.log(res.data));
This is due in large part to the design of RESTful endpoints. A
GET
request is specifically (and exclusively) for retrieving data from a server. As such, the only information that needs to be sent to the server - according to general practice - is the URI of the entity and any result augmenting parameters in the URL (sort, filter, limit, etc.). Including a body in a request is reserved for when actual data - not result augmentation - needs to be sent and acted upon by the server (POST
,PUT
,PATCH
). This is the same reason whyDELETE
doesn’t accept a payload. Everything the server needs to know about the entity to be deleted should be in the URL. Hope that helps clarify things 😀get
useconfig.params
or write in url querypost
useconfig.data
Any update on this ?
axios.get(URL, { params: { someData: data} })
and SS you can receive that as
req.query
The spec doesn’t forbid sending a payload for a get request, however both xhr and fetch won’t allow it so it is probably not possible to do this feature.
Reproduction:
fetch('/users', { method: 'GET', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: 'Hubot', login: 'hubot', }) });
: type errorvar a = new XMLHttpRequest(); a.open('GET', '/'); a.send('test');
: won’t send dataThey both work fine with POST
This way can be work. set the source and source_content_type params
So this issue is not yet solved 3 years later?? If you want to see the problem, here it is in its simplest form. The following curl works without an issue:
I think the equivalent axios call is:
I tried many different ways including params, stringifying the data, etc. Nothing seems to work and I’m unable to make this request work. Always getting some error. It may be a server implementation issue, but I have no control over the server, and I just need to issue the call like curl does so it simply works… Any idea for a workaround?
Yes, it appears there is no way to send data with a GET request from the browser…
But it is possible with node.js: https://nodejs.org/api/http.html#http_http_request_options_callback Note that after the http.request() call, the caller may call req.write(), before calling req.end().
And some popular services take advantage of data with a GET request. For example, see Elastic Search: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html
This is not possible. XHR clears the body when running
send
method. Even usingfetch
won’t work, for example chrome will give you Type Error.axios.get("someurl", {data: {some: "data"}})
seems to work fine. It sends the data in request body on Node.I was looking for the same feature, XHR should allow HTTP GET payload. IMO, It doesn’t make much sense not allowing sending payload as the body of a HTTP GET request as far as it doesn’t change the state of the server, QueryString or paybload are the same (just data). I stumbled across the issue when the payload is very large (> the max allowed URL length).
Update The RFC2616 referenced as “HTTP/1.1 spec” is now obsolete. In 2014 it was replaced by RFCs 7230-7237. Quote “the message-body SHOULD be ignored when handling the request” has been deleted. It’s now just “Request message framing is independent of method semantics, even if the method doesn’t define any use for a message body” The 2nd quote “The GET method means retrieve whatever information … is identified by the Request-URI” was deleted.
I think axios should add the data to the request parameters by himself. If to do it manually, then … in general, everything can be done manually …
The XHR API specification states that the body will get ignored when using the GET method:
Source: https://xhr.spec.whatwg.org/#the-send()-method
This applies to browser requests. However, as @psnider states, it should still work with HTTP requests from node.js, which are also supported by axios.
The old HTTP specification kind of hinted to try and avoid sending semantic information in the body of GET requests, but it doesn’t any more as far as I know: https://tools.ietf.org/html/rfc7231#section-4.3.1
@Franke123 exactly. Contributions are always welcome 😉
I dont see anything in the body:
Hi, I test it’s ok to use GET method send body to server from the browser…
I tried modify the lib/core/Axios.js:65 replace utils.forEach([‘delete’, ‘get’, ‘head’, ‘options’], function forEachMethodNoData(method) { to utils.forEach([‘delete’', ‘head’, ‘options’], function forEachMethodNoData(method) {
and replace utils.forEach([‘post’, ‘put’, ‘patch’], function forEachMethodWithData(method) { to utils.forEach([‘get’, ‘post’, ‘put’, ‘patch’], function forEachMethodWithData(method) {
and I want add a Axios config option to support this feature, can I?
As this is something that comes up (even for my reference) - axios doesn’t support url encoding of objects sent via GET. I don’t know why, but it doesn’t. To accomplish what you’re after you can do:
This will require qs
or in the File ./config/config.default.js : config.cors = { origin: [“https://localhost:8443”], allowedHeaders: [ “Content-Type”, “Authorization”, “Access-Control-Allow-Methods”, “Access-Control-Request-Headers” ], credentials: true, enablePreflight: true };
On Wed, Feb 19, 2020 at 2:04 AM Sagit Gutierrez notifications@github.com wrote:
– Nasser Ben oudi Swindonstraße 135 38226 Salzgitter Mobil: +49 (0)1515 6363 543 E-Mail: n.benoudi@gmail.com
@QuantumInformation can you go into a little more detail on your issue? Also @waspar, @umair-khanzada, @codeporan please see the following extract from RFC 7231 “A payload within a GET request message has no defined semantics; sending a payload body on a GET request might cause some existing implementations to reject the request”.
Since the semantics are undefined and most servers that handle a
GET
request will just disregard the body anyways I don’t think any change should be made here. If you are suggesting that theaxiso.get(...)
or other methods of usingaxios
to performGET
requests should automatically change the passedbody
into aparams
object then I doubt that would be done as it would create possible confusion and un-needed complexity.@rubennorte it might take some more investigation but it looks like XHR doesn’t support sending data in GET requests. Not only this library.