I tried sending a POST request using Spring Boot 3.2.6 Http RestClient. The request fails when sending and object and passes when sending a string.

Works with string code:

    private final String roleBody = """
    {"ApplicationID":"XXXX","RolesValues":["ROLE"],"retainPreviousRoles":"true"}
    """;

        ResponseEntity<ApiResponse> result = restClient
            .post()
            .uri("dev/v1/app-roles")
            .accept(APPLICATION_JSON)
            .contentType(APPLICATION_JSON)
            .body(addAppRole)
            .body(roleBody)
            .retrieve()
            .toEntity(ApiResponse.class);

Debug output from turning on http client logging:

User-Agent: Apache-HttpClient/5.2.3 (Java/17.0.11)
"POST /dev/v1/app-roles HTTP/1.1[\r][\n]"
"Content-Type: application/json[\r][\n]"
"Accept: application/json[\r][\n]"
"Accept-Encoding: gzip, x-gzip, deflate[\r][\n]"
"Content-Length: 128[\r][\n]"
"Host: appregapi.bpglobal.com[\r][\n]"
"Connection: keep-alive[\r][\n]"
"User-Agent: Apache-HttpClient/5.2.3 (Java/17.0.11)[\r][\n]"
"[\r][\n]"
"{"ApplicationID":"XXXX","RolesValues":["ROLE"],"retainPreviousRoles":"true"}[\n]"
"HTTP/1.1 200 OK[\r][\n]"
"Transfer-Encoding: chunked[\r][\n]"
"Content-Type: application/json; charset=utf-8[\r][\n]"
"Content-Encoding: gzip[\r][\n]"
"Vary: Accept-Encoding[\r][\n]"
"Date: Sat, 08 Jun 2024 20:22:09 GMT[\r][\n]"
"[\r][\n]"
"44[\r][\n]"

Fails when an object code:

        AddAppRole addAppRole = AddAppRole.builder()
            .applicationId("XXXX")
            .rolesValues(List.of("ROLE"))
            .retainPreviousRoles("true")
            .build();

        ResponseEntity<ApiResponse> result = restClient
            .post()
            .uri("dev/v1/app-roles")
            .accept(APPLICATION_JSON)
            .contentType(APPLICATION_JSON)
            .body(addAppRole)
            .retrieve()
            .toEntity(ApiResponse.class);

Debug output from turning on http client logging:

User-Agent: Apache-HttpClient/5.2.3 (Java/17.0.11)
"POST /dev/v1/app-roles HTTP/1.1[\r][\n]"
"Content-Type: application/json[\r][\n]"
"Accept: application/json[\r][\n]"
"Accept-Encoding: gzip, x-gzip, deflate[\r][\n]"
"Transfer-Encoding: chunked[\r][\n]"
"Host: appregapi.bpglobal.com[\r][\n]"
"Connection: keep-alive[\r][\n]"
"User-Agent: Apache-HttpClient/5.2.3 (Java/17.0.11)[\r][\n]"
"[\r][\n]"
"7f[\r][\n]"
"{"ApplicationID":"XXXX","RolesValues":["ROLE"],"retainPreviousRoles":"true"}[\r][\n]"
"0[\r][\n]"
"[\r][\n]"
"HTTP/1.1 403 Forbidden[\r][\n]"
"Transfer-Encoding: chunked[\r][\n]"
"Content-Type: application/json; charset=utf-8[\r][\n]"
"Date: Sat, 08 Jun 2024 20:23:35 GMT[\r][\n]"
"[\r][\n]"
"46[\r][\n]"
"{[\r][\n]"
HTTP/1.1 403 Forbidden

There appear differences in the payloads?

Any idea why the second version is adding: 7f \r \n and 0 \r \n

The different encoding is breaking the request.

Here is the AddAppRole class.

    @Builder
    @JsonPropertyOrder(value = {"ApplicationID", "RolesValues", "retainPreviousRoles"})
    public class AddAppRole {
        @JsonProperty("ApplicationID")
        private final String applicationId;

        @JsonProperty("RolesValues")
        private final List<String> rolesValues;

        private final String retainPreviousRoles;
    }

I noticed it was chunked, unsure how to stop it from being chunked with the new client or why this might alter the body.

Comment From: snicoll

The request fails when sending and object and passes when sending a string.

Thanks, but the logs of the client and a partial code snippet in text is not really actionable. It looks like it failed with a 403 so there's more to it than what you're showing. If you want support, please share a small sample application that reproduces the problem you've described. You can attach a zip to this issue or push the code to a GitHub repository.

Comment From: spring-projects-issues

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

Comment From: dmarsh26

I suspect its something to do with dependencies / auto wiring.

I tried to reproduce in a small project and it went away.

Bit busy right now to spend more time on it. Would need to gradually add in more dependencies and see if it came back.

thanks David


From: Spring Projects Issues @.> Sent: 17 June 2024 14:01 To: spring-projects/spring-framework @.> Cc: dmarsh26 @.>; Author @.> Subject: Re: [spring-projects/spring-framework] RestClient does not work when sending an object (Issue #32995)

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

— Reply to this email directly, view it on GitHubhttps://github.com/spring-projects/spring-framework/issues/32995#issuecomment-2173339954, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AB3JHPY4DY53GNWVM5DJGX3ZH3MZFAVCNFSM6AAAAABJCFKUZGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCNZTGMZTSOJVGQ. You are receiving this because you authored the thread.Message ID: @.***>

Comment From: snicoll

This should have been closed a while ago already. I've created https://github.com/spring-io/issue-bot/issues/44 to figure things out.

@dmarsh26 if you can provide the sample we requested, we can reopen and give it another go.