Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to support patch rest request with protobuf 3

Tags:

We often have use cases where we only want to update a subset fields on a resource. So if we have a resource Person:

type Person struct {
    Age int
    Name string
    Otherfield string
}

Say the calling client only wants to update the Age field. How would an endpoint be normally set up to handle this?

I believe this should be done with a PATCH request, with only the fields being set as part of the payload, ie:

{
    Age: 21
}

However, this won't work with proto3 because as far as I know there are no null fields, only default values. This won't work in many cases where the default value is valid.

like image 709
Dave Avatar asked Jun 13 '17 06:06

Dave


People also ask

Can you use protobuf with REST API?

Yes, you can absolutely combine Protobuf and REST. Protbuf specifies a way to encode data. REST specifies a way to interact with resources, but does not require any particular encoding for the resource bodies.

How do you send a protobuf request?

To use protobuf when making a request to the server, set the HTTP Content-Type header to application/x-google-protobuf in the client code. The Java client library provides helper classes to internally create protobuf messages based on a proto format and returns the corresponding byte array.

Can I use protobuf in HTTP?

In real world usage, you'll share your proto file with the client which will then generate its code files in the programming language of its choice. Create a client.go file in client folder. The client should be even more straightforward to understand. We are using http.

Is protobuf faster than JSON?

TL;DR — encoding and decoding string-intensive data in JavaScript is faster with JSON than it is with protobuf. When you have structured data in JavaScript, which needs to be sent over the network (for another microservice for example) or saved into a storage system, it first needs to be serialized.


1 Answers

Looking at Google own protobuf files (e.g. here), they use FieldMask for partial update.

FieldMask object is passed along with the request, and has the form (in JSON):

{
   mask: "Person.Age"
}

This allows the client to tell the server which fields they wish to update, without counting on the partial message itself to figure this out.

I think this adds unnecessary complexity on (each!) client, but we couldn't find any other way to achieve partial updates with proto3.

You can see full documentation of FieldMask here.

Note that it can also be used to filter out responses if the client doesn't need the entire object.

like image 159
danbars Avatar answered Oct 04 '22 16:10

danbars