What exactly is an HTTP Entity?

后端 未结 10 678
盖世英雄少女心
盖世英雄少女心 2020-11-29 16:31

Would someone please describe to me what exactly an HTTP entity is?

I am reading the HTTPClient documentation, but I do not really understand what t

相关标签:
10条回答
  • 2020-11-29 16:53

    It is an abstraction representing a request or response payload. The JavaDoc is clear on its purpose and various entity types.

    0 讨论(0)
  • 2020-11-29 16:55

    I guess the HTTPClient Entity is named according to HTTP Entity.

    0 讨论(0)
  • 2020-11-29 16:57

    An HTTP entity is the majority of an HTTP request or response, consisting of some of the headers and the body, if present. It seems to be the entire request or response without the request or status line (although only certain header fields are considered part of the entity).

    To illustrate; here's a request:

    POST /foo HTTP/1.1          # Not part of the entity.
    Content-Type: text/plain    # ┬ The entity is from this line down...
    Content-Length: 1234        # │
                                # │
    Hello, World! ...           # ┘
    

    And a response:

    HTTP/1.1 200 OK             # Not part of the entity.
    Content-Length: 438         # ┬ The entity is from this line down...
    Content-Type: text/plain    # │
                                # │
    Response body ...           # ┘
    
    0 讨论(0)
  • 2020-11-29 16:59

    HttpEntity is what you are going to pass in Request(with header) and what you are getting in Response. For Get Request we are passing simple String

     HttpHeaders headers = new HttpHeaders();
     headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
     HttpEntity<String> entity = new HttpEntity<String>(headers);
    

    For Post We are going to pass complete Entity Class

    public String createProducts(@RequestBody Product product) {
        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
        HttpEntity<Product> entity = new HttpEntity<Product>(product,headers);
    
        return restTemplate.exchange(
                 "http://localhost:8080/products", HttpMethod.POST, entity, String.class
               ).getBody();
    }
    
    0 讨论(0)
  • 2020-11-29 17:08

    As said in a comment by @hawkeye-parker, it looks like Entity has been deprecated. Make a search in this 2014 rfc, and you will see about XML entities and message body, but nothing about Http entity.

    Nevertheless, HttpClient, but also JaxRS client, have a setEntity() and getEntity() method.

    Considering the accepted answer, both libraries are wrong ! HttpClient.setEntity() won't remove previously set headers.

    0 讨论(0)
  • 2020-11-29 17:11

    HTTP is a Protocol which is observed when accessing information from a remote machine through a network. Usually the network is internet and the remote machine is a server.

    When you ask for information from person A to person B, you give him a message. (Request). Person B replies to you (Response). Request and Response are HTTP Message Types.

    Person A can ask Person B to do something, instead of asking for information. Say, Person A wants Person B to store a file in a secure location. So, Person A passes that file(HTTP Entity) to Person B and ask him to do something(HTTP Message). In this case, Person is passing an "Entity". In the context of HTTP Entity, it is a payload attached with the message.

    Hope the analogy helped.

    0 讨论(0)
提交回复
热议问题