简体   繁体   English

如何使用GET发送正文请求

[英]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). 我正在使用angularjs,并且必须通过服务使用路径参数(id),查询参数(var1和var2) 和主体请求 ({“ codes”:[“ 1000”]对服务器进行调用})-必须在get方法中作为字符串数组发送(我知道,发送正文请求应在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. 是的,您可以使用GET发送请求正文,但这没有任何意义。 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: 您可以在服务器上解析它并根据其内容修改响应,而在HTTP / 1.1规范的第4.3节中忽略了此建议

As far as I know, the standard has no statement about the body of a get type request. 据我所知,该标准没有关于get类型请求主体的声明。 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: XMLHttpRequest是其中之一:

send() accepts an optional parameter which lets you specify the request's body; send()接受一个可选参数,该参数使您可以指定请求的正文; this is primarily used for requests such as PUT. 这主要用于PUT之类的请求。 If the request method is GET or HEAD, the body parameter is ignored and the request body is set to null. 如果请求方法是GET或HEAD,则将忽略body参数,并将请求主体设置为null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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