From 680c5a456f0b4d993a2d87259113868323f99802 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C2=A0TheSmartnik?= Date: Wed, 31 Oct 2018 07:56:06 +0300 Subject: [PATCH] Add more examples --- examples/README.md | 11 +++++++++++ examples/body_stream.rb | 14 ++++++++++++++ examples/multipart.rb | 22 ++++++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 examples/body_stream.rb create mode 100644 examples/multipart.rb diff --git a/examples/README.md b/examples/README.md index d4a803a..6ddbd9e 100644 --- a/examples/README.md +++ b/examples/README.md @@ -70,3 +70,14 @@ * Uses `get` requests * Uses `stream_body` mode * Download file without using the memory + +* [Microsoft graph](microsoft_graph.rb) + * Basic Auth + * Uses `post` requests + * Uses multipart + +* [Multipart](multipart.rb) + * Multipart data upload _(with and without file)_ + +* [Uploading File](body_stream.rb) + * Uses `body_stream` to upload file diff --git a/examples/body_stream.rb b/examples/body_stream.rb new file mode 100644 index 0000000..8ca405e --- /dev/null +++ b/examples/body_stream.rb @@ -0,0 +1,14 @@ +# To upload file to a server use :body_stream + +HTTParty.put( + 'http://localhost:3000/train', + body_stream: File.open('sample_configs/config_train_server_md.yml', 'r') +) + + +# Actually, it works with any IO object + +HTTParty.put( + 'http://localhost:3000/train', + body_stream: StringIO.new('foo') +) diff --git a/examples/multipart.rb b/examples/multipart.rb new file mode 100644 index 0000000..24c6916 --- /dev/null +++ b/examples/multipart.rb @@ -0,0 +1,22 @@ +# If you are uploading file in params, multipart will used as content-type automatically + +HTTParty.post( + 'http://localhost:3000/user', + body: { + name: 'Foo Bar', + email: 'example@email.com', + avatar: File.open('/full/path/to/avatar.jpg') + } +) + + +# However, you can force it yourself + +HTTParty.post( + 'http://localhost:3000/user', + multipart: true, + body: { + name: 'Foo Bar', + email: 'example@email.com' + } +)