简体   繁体   中英

How to send a body request with GET

I'm working with angularjs, and from my service I have to make a call to the server using a path parameter (id), query params (var1 and var2) and a body request ({"codes": ["1000"]}) - which has to be sent as an string array, within a get method (I know, sending a body request should be done within a POST).

So far, in my service I have this:

        function getSub(id, var1, var2) {
           var payload = {
               first: var1,
               second: var2
           };

           var url = 'sub/' + id + '/mylink'
           return api.get(url, payload, {"codes": ["1000"]}).then(function (response) {
               console.log("READ RESPONSE ", response);
               return response;
           });
        };

So far, all I am getting is a bad request error linked to the response body not provided.

It may be a noob question, and not a best practice one, but I meed to find a solution for this. So far, by searching the net far and wide, I could only understand that this is an unorthodox way of using body request.

Thanks in advance!

Yes, you can send a request body with GET but it make no sense. you can parse it on the server and modify your response based on its contents, you are ignore this recommendation in the HTTP / 1.1 specification, section 4.3:

As far as I know, the standard has no statement about the body of a get type request. Thus this is a classical it depends on the implementation . Anyway, implementations tend not to support such a combination. And XMLHttpRequest is one of them:

send() accepts an optional parameter which lets you specify the request's body; this is primarily used for requests such as PUT. If the request method is GET or HEAD, the body parameter is ignored and the request body is set to null.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM