grpc-ecosystem/grpc-gateway

Handling Binary File Uploads

Open

#500 opened on Dec 12, 2017

View on GitHub
 (24 comments) (26 reactions) (0 assignees)Go (16,971 stars) (2,250 forks)batch import
documentationhelp wanted

Description

Hi,

I have a protobuf that defines one field

message FileUpload { bytes fileContents = 1; }

I want to be able to upload a file, and have the contents of that upload stored in fileContents. When sending json data, I understand that the keys map to grpc message fields. But in the case of a binary upload there is no field name, so how can I map the contents of the request body into a message?

rpc UploadFile(FileUpload) returns (google.protobuf.Empty)) {
    option (google.api.http) = {
        post: "/v1/files"
        body: "*"
    };
}

If I use curl to make a request like this: curl -X POST --data-binary "@/tmp/test_file.txt" http://localhost:9090/v1/files

The HTTP request looks like this:

POST /v1/cmd/secretfiles HTTP/1.1
Host: localhost:9090
User-Agent: curl/7.54.0
Accept: */*
Content-Length: 1675
Content-Type: application/x-www-form-urlencoded
Expect: 100-continue

uEH2KxtiW5Tpuj7bU5hmPO1h4QZuCcrrNDA/OvNyYmw0U02Uy/mR5iWnS6tMdPmZ
iJBmN06MZ2Khx5rV+rqvwF9CUGYzja/dDCm+2wIDAQABAoIBAB050/iJ0YpyGtig
hRPQ7IetKx8HRfJYLSlYu9+eo4/e7EGAfm1dZWz9pJO0kcnUCn3iOKZUDqdGqIz0
9mXR0wK8DaWgYFf0Wx9d8/EEOdUUrQ9Eh82CCWk94fbCbC/b1NdZ90DaUhIZ1J7y
keCiyuPj5rClhrAdf/GYy8bEXp2W9+zi5vH2dTi3JDe2rImh+urEjCYThV3dxoms
HrwX+nWrwNVXfAIJ3R8ojRoiFQtckYgSytyyYdGaxvaZf4rw3CWK+4U1b6NiC4XP
IDVpBfWSXsbPvliA4A35F/15Let/ASw4YZiUryrsYBVNMBMjiydULFvNX0WQVVJE

Contributor guide