我正在为我们的应用程序开发一个新的RESTful Web服务。

当对某些实体执行GET时,客户端可以请求实体的内容。如果他们想添加一些参数(例如排序列表),可以在查询字符串中添加这些参数。

或者,我希望人们能够在请求主体中指定这些参数。HTTP/1.1似乎并没有明确禁止这一点。这将允许他们指定更多信息,可能会更容易指定复杂的XML请求。

我的问题:

这完全是个好主意吗?HTTP客户端在GET请求中使用请求体时会遇到问题吗?

https://www.rfc-editor.org/rfc/rfc2616


当前回答

根据XMLHttpRequest,它无效。根据标准:

4.5.6 send()方法客户send([正文=空])启动请求。可选参数提供请求身体如果请求方法是GET或HEAD,则忽略该参数。如果任一状态都不是,则引发InvalidStateError异常打开或设置send()标志。send(body)方法必须运行以下步骤:如果状态未打开,则引发InvalidStateError异常。如果设置了send()标志,则引发InvalidStateError异常。如果请求方法是GET或HEAD,请将body设置为null。如果body为空,则转到下一步。

虽然,我认为这不应该,因为GET请求可能需要大量的正文内容。

因此,如果您依赖浏览器的XMLHttpRequest,它很可能无法工作。

其他回答

根据XMLHttpRequest,它无效。根据标准:

4.5.6 send()方法客户send([正文=空])启动请求。可选参数提供请求身体如果请求方法是GET或HEAD,则忽略该参数。如果任一状态都不是,则引发InvalidStateError异常打开或设置send()标志。send(body)方法必须运行以下步骤:如果状态未打开,则引发InvalidStateError异常。如果设置了send()标志,则引发InvalidStateError异常。如果请求方法是GET或HEAD,请将body设置为null。如果body为空,则转到下一步。

虽然,我认为这不应该,因为GET请求可能需要大量的正文内容。

因此,如果您依赖浏览器的XMLHttpRequest,它很可能无法工作。

restclient和REST控制台都不支持这一点,但curl支持。

HTTP规范在第4.3节中说明

如果请求方法规范(第5.1.1节)不允许在请求中发送实体体,则请求中不得包含消息体。

第5.1.1节将我们重定向到第9.x节以了解各种方法。它们都没有明确禁止包含消息体。然而

第5.2节规定

通过检查请求URI和主机头字段来确定由Internet请求标识的确切资源。

第9.3节规定

GET方法意味着检索请求URI标识的任何信息(以实体的形式)。

这一起表明,在处理GET请求时,服务器不需要检查除请求URI和主机头字段之外的任何内容。

总之,HTTP规范不会阻止您使用GET发送消息体,但存在足够的歧义,如果不是所有服务器都支持它,我不会感到惊讶。

IMHO,您只需在URL中发送JSON编码(即encodeURIComponent),这样就不会违反HTTP规范并将JSON发送到服务器。

创建Requestfactory类

import java.net.URI;

import javax.annotation.PostConstruct;

import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpUriRequest;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
public class RequestFactory {
    private RestTemplate restTemplate = new RestTemplate();

    @PostConstruct
    public void init() {
        this.restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestWithBodyFactory());
    }

    private static final class HttpComponentsClientHttpRequestWithBodyFactory extends HttpComponentsClientHttpRequestFactory {
        @Override
        protected HttpUriRequest createHttpUriRequest(HttpMethod httpMethod, URI uri) {
            if (httpMethod == HttpMethod.GET) {
                return new HttpGetRequestWithEntity(uri);
            }
            return super.createHttpUriRequest(httpMethod, uri);
        }
    }

    private static final class HttpGetRequestWithEntity extends HttpEntityEnclosingRequestBase {
        public HttpGetRequestWithEntity(final URI uri) {
            super.setURI(uri);
        }

        @Override
        public String getMethod() {
            return HttpMethod.GET.name();
        }
    }

    public RestTemplate getRestTemplate() {
        return restTemplate;
    }
}

和@Autowired,这里是一个带有RequestBody的GET请求示例代码

 @RestController
 @RequestMapping("/v1/API")
public class APIServiceController {
    
    @Autowired
    private RequestFactory requestFactory;
    

    @RequestMapping(method = RequestMethod.GET, path = "/getData")
    public ResponseEntity<APIResponse> getLicenses(@RequestBody APIRequest2 APIRequest){
        APIResponse response = new APIResponse();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        Gson gson = new Gson();
        try {
            StringBuilder createPartUrl = new StringBuilder(PART_URL).append(PART_URL2);
            
            HttpEntity<String> entity = new HttpEntity<String>(gson.toJson(APIRequest),headers);
            ResponseEntity<APIResponse> storeViewResponse = requestFactory.getRestTemplate().exchange(createPartUrl.toString(), HttpMethod.GET, entity, APIResponse.class); //.getForObject(createLicenseUrl.toString(), APIResponse.class, entity);
    
            if(storeViewResponse.hasBody()) {
                response = storeViewResponse.getBody();
            }
            return new ResponseEntity<APIResponse>(response, HttpStatus.OK);
        }catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<APIResponse>(response, HttpStatus.INTERNAL_SERVER_ERROR);
        }
        
    }
}

我向IETF HTTP工作组提出了这个问题。Roy Fielding(1998年,http://1.1文档的作者)的评论是

“…一个实现将被破坏,无法执行除解析和丢弃该主体之外的任何操作”

RFC 7213(HTTPbis)规定:

“GET请求消息中的有效负载没有定义的语义;”

现在看来很明显,其意图是禁止GET请求体上的语义,这意味着请求体不能用于影响结果。

如果你在GET上包含一个主体,那么有一些代理肯定会以各种方式破坏你的请求。

总之,不要这样做。