Enhanced HTTP Standard Capabilities of Triple - Multi Content-Type Support

This article mainly introduces how Triple supports more standard HTTP Content-Types and how services should handle these requests.

Enhanced HTTP Standard Capabilities of Triple - Multi Content-Type Support

This article mainly introduces how Triple supports more standard HTTP Content-Types and how services should handle these requests.

Overview

Triple currently supports two serialization methods: Json and protobuf, corresponding to the ContentTypes:

  • application/json
  • application/grpc+proto

This poses no problem when both consumers and providers are backend services. However, for browser clients, they may send more types of ContentTypes that the server needs to support for decoding, such as:

  • multipart/formdata
  • text/plain
  • application/x-www-form-urlencoded
  • application/xml

Rest has basically implemented the above decoding capabilities, making it an important step for Triple to achieve complete interoperability between Triple servers and browser clients.

Usage

multipart/formdata
  1. POST /org.apache.dubbo.samples.tri.noidl.api.PojoGreeter/greetPojo HTTP/1.1
  2. Host: 192.168.202.1:50052
  3. Content-Type: multipart/form-data; boundary=example-part-boundary
  4. Accept: application/json
  5. --example-part-boundary
  6. Content-Disposition: form-data; name="username"
  7. Content-Type: text/plain
  8. LuYue
  9. --example-part-boundary
  10. Content-Disposition: form-data; name="userdetail"
  11. Content-Type: application/json
  12. {
  13. "location":"beijing",
  14. "username":"LuYue"
  15. }
  16. --example-part-boundary
  17. Content-Disposition: form-data; name="userimg";filename="user.jpeg"
  18. Content-Type: image/jpeg
  19. <binary-image data>
  20. --example-part-boundary--

Receiving:

  1. @Override
  2. public ServerResponse greetPojo(String username, User user, byte[] attachment) {
  3. //LuYue
  4. System.out.println(username);
  5. //user.name=Luyue;user.location=beijing
  6. System.out.println(user);
  7. //<binary-image data>
  8. System.out.println(new String(attachment, StandardCharsets.UTF_8));
  9. return new ServerResponse("Server Received:"+username);
  10. }
  • Each part is decoded based on its Content-Type.
  • If the method parameter is byte[] or Byte[], the corresponding field will not be decoded.
  • The response is encoded using application/json.
application/x-www-form-urlencoded
  1. POST /org.apache.dubbo.samples.tri.noidl.api.PojoGreeter/greetUrlForm HTTP/1.1
  2. Host: 192.168.202.1:50052
  3. Content-Type: application/x-www-form-urlencoded
  4. Content-Length: 33
  5. Accept: application/json
  6. Hello=World&Apache=Dubbo&id=10086

Two receiving methods:

  1. public ServerResponse greetUrlForm(String hello,String apache,long id){
  2. System.out.println("Hello:"+hello);
  3. System.out.println("Apache:"+apache);
  4. System.out.println("Id:"+id);
  5. return new ServerResponse("Server Received url form");
  6. }
  1. public ServerResponse greetUrlForm(Map<String,Object> params){
  2. System.out.println("Hello:"+params.get("Hello"));
  3. System.out.println("Apache"+params.get("Apache"));
  4. System.out.println("Id"+params.get("Id"));
  5. return new ServerResponse("Server Received url form");
  6. }
  • If the parameter is a Map, it is decoded as Map<String,String>.
  • If the parameters are all String or numeric types, they are decoded one by one according to the parameter list.
  • The response is encoded using application/json.
text/plain
  1. POST /org.apache.dubbo.samples.tri.noidl.api.PojoGreeter/greetString HTTP/1.1
  2. Host: 192.168.202.1:50052
  3. Content-Type: text/plain; charset=UTF-8
  4. Content-Length: 6
  5. Accept: application/json
  6. World!

Receiving:

  1. public ServerResponse greetUrlForm(String world){
  2. System.out.println("Hello:"+ world);
  3. return new ServerResponse("Server Received url form.");
  4. }
  • Charset supports ASCII, UTF-8, UTF-16, etc., defaulting to UTF-8.
  • The response is encoded using application/json.
application/xml
  1. POST /org.apache.dubbo.samples.tri.noidl.api.PojoGreeter/greetXml HTTP/1.1
  2. Host: 192.168.202.1:50052
  3. Content-Type: application/xml
  4. Content-Length: 86
  5. Accept: application/xml
  6. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  7. <User>
  8. <username>JohnDoe</username>
  9. <location>New York</location>
  10. </User>

Receiving:

  1. @Override
  2. public ServerResponse greetXml(User user) {
  3. System.out.println(user.getUsername());
  4. System.out.println(user.getLocation());
  5. return new ServerResponse("Server Received xml.");
  6. }
  • This implementation is the same as Rest’s XMLCodec.
  • The response is encoded using application/xml.

Feedback

Was this page helpful?

Yes No

Last modified September 30, 2024: Update & Translate Overview Docs (#3040) (d37ebceaea7)