The Wayback Machine - http://web.archive.org/web/20210725151225/https://github.com/line/armeria/issues/3707
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

Provide a way to send a JSON object with WebClient #3707

Open

ikhoon opened this issue Jul 19, 2021 · 5 comments
Open

Provide a way to send a JSON object with WebClient #3707

ikhoon opened this issue Jul 19, 2021 · 5 comments

Comments

@ikhoon
Copy link
Contributor

@ikhoon ikhoon commented Jul 19, 2021

JSON is the dominant exchange format for REST API.
However, Armeria's WebClient does not provide any JSON-specific APIs.
I believe it should be useful additions if we provide:

WebClient client = WebClient.of();
// Send a serialized JSON object with "applicaiton/json"
HttpResponse response = client.postJson("/items", new MyItem()); 
HttpResponse response = client.putJson("/items", new MyItem());
HttpResponse response = client.patchJson("/items", new MyItem());

HttpResponse response = client.prepare()
                              .post("/items")
                              .contentJson(new MyItem())
                              .execute();
@ghkim3221
Copy link
Contributor

@ghkim3221 ghkim3221 commented Jul 19, 2021

Additionally, how do you think similar feature, HttpRequest.ofJson?
Additionally, how about adding HttpRequest.ofJson also?

@trustin
Copy link
Collaborator

@trustin trustin commented Jul 19, 2021

+1 to having HttpRequest.ofJson() as well.

@ikhoon
Copy link
Contributor Author

@ikhoon ikhoon commented Jul 20, 2021

It would also be nice to have APIs that automatically convert a response into an object.

  • Content-Type
    • We can support well-known media types such as:
      • "application/json"
      • "application/protobuf" (We can add this later cause Protobuf is not a popular format for REST.)
    • An UnsupportedMediaTypeException could complete the returned future for unknown media types.
    • An JsonProcessingException could complete the returned future
      when failing to decode a JSON in response body into an object.
  • Status
    • The default expected HTTP response is 200 OK. If another status is received;
    • UnexpectedHttpStatusException could complete the returned future.
    • Alternatively, an additional API that can specify an expected HTTP status.
interface HttpResponse {
  <T> CompletableFuture<T> aggregateAs(Class<? extends T> clazz);
  <T> CompletableFuture<T> aggregateAs(HttpStatus expectedStatus, Class<? extends T> clazz);
  <T> CompletableFuture<T> aggregateAs(HttpStatus expectedStatus, Class<? extends T> clazz, EventExecutor executor);
}
@karellen-kim
Copy link

@karellen-kim karellen-kim commented Jul 20, 2021

I'm interested in this!

@ikhoon
Copy link
Contributor Author

@ikhoon ikhoon commented Jul 20, 2021

Thanks! It's all yours.

karellen-kim pushed a commit to karellen-kim/armeria that referenced this issue Jul 21, 2021
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
4 participants