Unable to set request body optionally #529
Replies: 4 comments
-
It would if you configured an Object Mapper to understand what an Optional
is. Otherwise it’s just going to call toString on it.
…On Sun, Aug 11, 2024 at 7:54 PM bensullivan-ag ***@***.***> wrote:
Hi
Should the following pass?
@test
void shouldAllowRequestBodiesToBeAppliedConditionally() {
var maybeBody = Optional.of("test body");
var request = Unirest.patch("api.com").header("Authorization", "Bearer XXXXXX");
maybeBody.ifPresent((b) -> request.header("Content-Type", "application/json").body(b));
assertTrue(request.getBody().isPresent());
assertEquals(maybeBody.get(), request.getBody().get());
}
Thanks
—
Reply to this email directly, view it on GitHub
<#529>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AABDPT54FGB23KO5PQ5T6V3ZRABVDAVCNFSM6AAAAABMLGYBMOVHI2DSMVQWIX3LMV43ERDJONRXK43TNFXW4OZXGA2DANJZHE>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Thanks for reply. Which line are you referring to?
|
Beta Was this translation helpful? Give feedback.
-
Sorry I was reading it from my phone, I thought you were passing the Optional as the body. You don't even need the Optional at all to produce the same result. It could be this:
but the answer is no, this would not work. "Test body" is a string, |
Beta Was this translation helpful? Give feedback.
-
Oh another thing, the Unirest request object goes though different transformations as you build it. What is returned from .body("") is a DIFFERENT object from your request object. So the original "request" variable won't have a body, because it is a previous version of the chain. |
Beta Was this translation helpful? Give feedback.
-
Hi
Should the following pass?
Thanks
Beta Was this translation helpful? Give feedback.
All reactions