Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OkHttpClientHttpRequestFactory should allow POST requests without body [SPR-15015] #19582

Closed
spring-projects-issues opened this issue Dec 14, 2016 · 1 comment
Assignees
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Dec 14, 2016

Mark Paluch opened SPR-15015 and commented

OkHttp 2 and 3 require a request body for certain HTTP methods (PUT, POST, PATCH, …). OkHttpClientHttpRequestFactory and OkHttp3ClientHttpRequestFactory passes a null argument as body if the request entity is null or the content is empty. The empty-body check prevents a workaround by passing in an empty byte[].

Exception:

java.lang.IllegalArgumentException: method POST must have a request body.

	at okhttp3.Request$Builder.method(Request.java:236)
	at org.springframework.http.client.OkHttp3ClientHttpRequestFactory.buildRequest(OkHttp3ClientHttpRequestFactory.java:138)
	at org.springframework.http.client.OkHttp3ClientHttpRequest.executeInternal(OkHttp3ClientHttpRequest.java:67)
	at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
	at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)
	at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:619)
	at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:580)
	at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:407)	

Test code:

@Test
public void shouldThrowIllegalArgumentExceptionForEmptyBody() throws Exception {

	RestTemplate restTemplate = new RestTemplate(new OkHttp3ClientHttpRequestFactory());

	restTemplate.postForEntity("http://www.google.com", new byte[0], String.class);
}

@Test
public void shouldThrowIllegalArgumentExceptionForNullBody() throws Exception {

	RestTemplate restTemplate = new RestTemplate(new OkHttp3ClientHttpRequestFactory());

	restTemplate.postForEntity("http://www.google.com", null, String.class);
}

Proposed fix:

The proposed fix (see OkHttp #2651) coerces an absent body into an empty body.
OkHttp provides internal APIs to determine whether a request body is required. The fix would use HttpMethod.requiresRequestBody(…) to determine whether to create an empty body if the request entity is null or the content is empty.


Affects: 4.2.4, 4.3 GA

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Dec 14, 2016

Juergen Hoeller commented

Since all other ClientHttpRequestFactory implementations do allow for POST requests without a body, this as a consistency issue that we need to resolve for 4.3.5. Since we have other limitations in the ClientHttpRequestFactory implementations in the 4.2.x line still that have only been resolved in 4.3.x and since we need an internal OkHttp class here, I'll rather leave this change for 4.3.x only.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.