Remove Transfer-Encoding:chunked in the POST request?

programmingtech

I am sending a POST request using the following code but the request is send in the form of chunked (Transfer-Encoding: chunked). I googled about the problem and it says to include the Content-Length but in the below code I could not figure out how could I set the Content-Length:

@RequestMapping(value = "/contacts", method = RequestMethod.POST)
public Map<String, ContactInfo> addContactInfo(
                                @RequestBody Map<String, ContactInfo> ContactInfoDto) {

    ContactInfo contactInfo = ContactInfoDto.get("contact");
    if (contactInfo == null) {
        throw new IllegalArgumentException("Contact not found.");
    }

    contactInfo = this.contactInfoManager.addNew(contactInfo);
    Map<String, ContactInfo> map = new HashMap<>();
    map.put("contact", contactInfo);

    return map;

}
mekazu

You can use ResponseEntity to set headers explicitly. The tricky bit is figuring out how long your content actually is:

@RequestMapping(value = "/contacts", method = RequestMethod.POST)
public ResponseEntity<Map<String, ContactInfo>> addContactInfo(@RequestBody Map<String, ContactInfo> contactInfoDto) throws JsonProcessingException {

    ContactInfo contactInfo = contactInfoDto.get("contact");
    if (contactInfo == null) {
        throw new IllegalArgumentException("Contact not found.");
    }

    contactInfo = this.contactInfoManager.addNew(contactInfo);
    Map<String, ContactInfo> map = new HashMap<>();
    map.put("contact", contactInfo);

    HttpHeaders headers = new HttpHeaders();
    headers.set(HttpHeaders.CONTENT_LENGTH, String.valueOf(new ObjectMapper().writeValueAsString(map).length()));
    return new ResponseEntity<Map<String, ContactInfo>>(map, headers, HttpStatus.CREATED);
}

Test:

$ curl -v http://localhost:8080/contacts/ -X POST -d '{ "contact": { "name": "foo" } }' -H 'Content-Type: application/json' && echo
*   Trying ::1...
* Connected to localhost (::1) port 8080 (#0)
> POST /contacts/ HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.43.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 32
> 
* upload completely sent off: 32 out of 32 bytes
< HTTP/1.1 201 Created
< Server: Apache-Coyote/1.1
< X-Application-Context: application
< Content-Type: application/json;charset=UTF-8
< Content-Length: 26
< Date: Fri, 10 Jun 2016 13:24:23 GMT
< 
* Connection #0 to host localhost left intact
{"contact":{"name":"foo"}}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Transfer-Encoding: chunked

From Dev

Transfer-Encoding: chunked

From Dev

Empty request body when Transfer-Encoding is Chunked - NancyFX

From Dev

nginx chunked transfer encoding fails

From Dev

Disable "Transfer-Encoding:chunked" in Apache httpd

From Dev

Chunked transfer encoding - Page is not displayed properly

From Dev

Who set the Transfer-Encoding: chunked header?

From Dev

Retrofit client and response with Transfer-Encoding: chunked

From Dev

POST request to PHP7 with chunked encoding does not properly return result

From Dev

Node.js: how to disable chunked transfer-encoding?

From Dev

Does Chunked Transfer Encoding work with Nancy, OWIN and HttpListener?

From Dev

How to read response using HttpsUrlconnection when transfer encoding is chunked

From Dev

Is it a good idea to use Transfer-Encoding: chunked on static files?

From Dev

Is there a wsgi server that will do progressive Transfer-Encoding: chunked

From Dev

GoogleTV Mediaplayer cannot handle http datasource with Transfer-Encoding : chunked

From Dev

How to read response using HttpsUrlconnection when transfer encoding is chunked

From Dev

How does HTTP specify the end of a response for Transfer-encoding: chunked?

From Dev

Content-Encoding: gzip + Transfer-Encoding: chunked with gzip/zlib gives incorrect header check

From Dev

Transfer-Encoding: Chunked causes 404 The system cannot find the file specified

From Dev

how to disable chunked-transfer-encoding in grizzly based http-server

From Dev

Disable chunked transfer-encoding for JAX-WS Client in WebSphere Application Server 8.5

From Dev

is the Chunked encoding working correctly?

From Dev

HTTP: Illegal chunked encoding

From Dev

Disable chunked transfer flowjs

From Dev

Access external database from Wordpress post: ERR_INCOMPLETE_CHUNKED_ENCODING

From Dev

Android Retrofit post request multipart encoding error

From Dev

net/http server does not send 100 Continue unless client sends Transfer-Encoding: chunked or nonzero Content-Length

From Dev

SoapUI - How to use Property Transfer with POST request

From Dev

What is a chunked request in OpenTSDB?

Related Related

  1. 1

    Transfer-Encoding: chunked

  2. 2

    Transfer-Encoding: chunked

  3. 3

    Empty request body when Transfer-Encoding is Chunked - NancyFX

  4. 4

    nginx chunked transfer encoding fails

  5. 5

    Disable "Transfer-Encoding:chunked" in Apache httpd

  6. 6

    Chunked transfer encoding - Page is not displayed properly

  7. 7

    Who set the Transfer-Encoding: chunked header?

  8. 8

    Retrofit client and response with Transfer-Encoding: chunked

  9. 9

    POST request to PHP7 with chunked encoding does not properly return result

  10. 10

    Node.js: how to disable chunked transfer-encoding?

  11. 11

    Does Chunked Transfer Encoding work with Nancy, OWIN and HttpListener?

  12. 12

    How to read response using HttpsUrlconnection when transfer encoding is chunked

  13. 13

    Is it a good idea to use Transfer-Encoding: chunked on static files?

  14. 14

    Is there a wsgi server that will do progressive Transfer-Encoding: chunked

  15. 15

    GoogleTV Mediaplayer cannot handle http datasource with Transfer-Encoding : chunked

  16. 16

    How to read response using HttpsUrlconnection when transfer encoding is chunked

  17. 17

    How does HTTP specify the end of a response for Transfer-encoding: chunked?

  18. 18

    Content-Encoding: gzip + Transfer-Encoding: chunked with gzip/zlib gives incorrect header check

  19. 19

    Transfer-Encoding: Chunked causes 404 The system cannot find the file specified

  20. 20

    how to disable chunked-transfer-encoding in grizzly based http-server

  21. 21

    Disable chunked transfer-encoding for JAX-WS Client in WebSphere Application Server 8.5

  22. 22

    is the Chunked encoding working correctly?

  23. 23

    HTTP: Illegal chunked encoding

  24. 24

    Disable chunked transfer flowjs

  25. 25

    Access external database from Wordpress post: ERR_INCOMPLETE_CHUNKED_ENCODING

  26. 26

    Android Retrofit post request multipart encoding error

  27. 27

    net/http server does not send 100 Continue unless client sends Transfer-Encoding: chunked or nonzero Content-Length

  28. 28

    SoapUI - How to use Property Transfer with POST request

  29. 29

    What is a chunked request in OpenTSDB?

HotTag

Archive