2

in this page it is described how to automatically support REST requests in an existing GRPC server

https://cloud.google.com/endpoints/docs/grpc/transcoding

But there is no mention for file upload support . For example is it possible to support requests such as this

curl -F "image=@/home/user1/Desktop/test.jpg" localhost/uploader.php

with this or similar methods ?

The grpc code handles files in this way :

Proto files:

service serviceName {
    ...
    rpc WriteApi(TarFile) returns (Status) { }
    ...
}

message TarFile {
    bytes contents = 1;
    string somethingElse =2;
 }

and in the serving function something like :

func (server *Server) WriteApi(ctx context.Context, jf1 *pb.TarFile) (*pb.Status, error) {
    buf1 := bytes.NewBuffer(jf1.Contents)
    tr1 := tar.NewReader(buf1)
    for {
        hdr1, err1 := tr1.Next()
        if err1 == io.EOF {
            break
        }
        if err1 != nil {
            log.Fatal(err1)
        }
        bud1 := new(bytes.Buffer)
        bud1.ReadFrom(tr1)
        s1 := bud1.String()
        l4g.Info(s1)
        ioutil.WriteFile(rseedstr+"/"+hdr1.Name, []byte(s1), 0644)
    }
demi
  • 21
  • 3
  • In your example, you are using a `multipart/form-data` request to submit the image. This is not a JSON request, so advice on translating JSON probably doesn't apply. JSON can't hold raw image data. If the image is small, you could b64 encode it and send it as a string. How does gRPC handle blob data? How do you want it to work? – QuinnFreedman Mar 07 '21 at 01:47
  • I have edited my question with more info about the grpc part. Meanwhile I have found this comment on an issue "Unfortunately at this moment we don't support and don't have a plan to support file uploads" in the grpc-gateway which is recently reopened https://github.com/grpc-ecosystem/grpc-gateway/issues/500 – demi Mar 07 '21 at 07:48

0 Answers0