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 docs for multipart request with in memory file contents #422

Merged
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
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,33 @@ Add that to the application supervisor and `first_pool` will be available to be

#### Request

HTTPoison supports making `multipart` requests. E.g.:
HTTPoison supports making `multipart` requests. E.g. with a local file:

```elixir
HTTPoison.post("https://myurl.php", {:multipart, [{:file, "test.txt", {"form-data", [{"name", "mytest"}, {"filename", "test.txt"}]}, []}]})
```

Sometimes you may already have the file contents in memory and want to upload
it elsewhere. A common example is fetching the file from a service like S3 and
uploading it somewhere else. There is no need to persist the file locally, you
can do the below:

```elixir
binary_file_content = "Something you fetched and now have it in memory"
token = "some_token_from_another_request"
headers = ["Authorization": "Bearer #{token}", {"Content-Type", "multipart/form-data"}]
options = [ssl: [{:versions, [:'tlsv1.2']}], recv_timeout: 500]

HTTPoison.request(
:post,
"https://myurl.com",
{:multipart,
[{"file", binary_file_content, {"form-data", [name: "file", filename: "a_file_name.txt"]}, []}]},
headers,
options
)
```

Further examples of `multipart` requests can be found [in the issues](https://github.com/edgurgel/httpoison/issues?utf8=%E2%9C%93&q=is%3Aissue+multipart) (e.g.: [here](https://github.com/edgurgel/httpoison/issues/144#issue-160035453) and [here](https://github.com/edgurgel/httpoison/issues/237#issuecomment-313132804)).

For more complex queries regarding multipart requests, you should follow the [hackney docs for the `multipart` API](https://github.com/benoitc/hackney#send-a-body).
Expand Down