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

Add doc with info about binary upload a custom route, for #500 #2063

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions docs/docs/mapping/binary_file_uploads.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
layout: default
title: Binary file uploads
nav_order: 2
parent: Mapping
---

# Binary file uploads

If you need to do a binary file upload, e.g. via;

```sh
curl -X POST -F "attachment=@/tmp/somefile.txt" http://localhost:9090/v1/files
```

then your request will contain the binary data directly and there is no way to model this using gRPC.

What you can do instead is to add a custom route directly on the `mux` instance.

## Custom route on a mux instance

Here we'll setup a handler (`handleBinaryFileUpload`) for `POST` requests:

```go
// Create a mux instance
mux := runtime.NewServeMux()

// Attachment upload from http/s handled manually
mux.HandlePath("POST", "/v1/files", handleBinaryFileUpload)
```

And then in your handler you can do something like:

```go
func handleBinaryFileUpload(w http.ResponseWriter, rq *http.Request, params map[string]string) {
err := r.ParseForm()
if err != nil {
http.Error(w, fmt.Sprintf("failed to parse form: %s", err.Error()), http.StatusBadRequest)
return
}

f, header, err := rq.FormFile("attachment")
if err != nil {
http.Error(w, fmt.Sprintf("failed to get file 'attachment': %s", err.Error()), http.StatusBadRequest)
return
}
jonathanbp marked this conversation as resolved.
Show resolved Hide resolved
defer f.Close()

//
// Now do something with the io.Reader in `f`, i.e. read it into a buffer or stream it to a gRPC client side stream.
// Also `header` will contain the filename, size etc of the original file.
//
}
```