Skip to content

Instantly share code, notes, and snippets.

@technoweenie
Last active January 8, 2020 19:11
Show Gist options
  • Save technoweenie/e026d53afcbe07ce7dda629d3549a3a1 to your computer and use it in GitHub Desktop.
Save technoweenie/e026d53afcbe07ce7dda629d3549a3a1 to your computer and use it in GitHub Desktop.
f01x = ENV['FARADAY_VERSION'] == '0.1x'
if f01x
puts 'loading faraday v0.17.x...'
require 'rubygems'
gem 'faraday', '~> 0.17.0'
else
puts 'loading latest faraday...'
end
require 'faraday'
require 'json'
puts Faraday::VERSION
BASE_URL = 'https://requestbin.io/17vs3fa1'
client = Faraday::Connection.new(url: BASE_URL) do |builder|
#builder.request :cookie_jar
builder.request :multipart
builder.request :url_encoded
builder.adapter :net_http
end
message = {title: "title", body: "body of message", recipientIdList:[186554]}
payload = if f01x
{ message: Faraday::UploadIO.new(StringIO.new(JSON.dump(message)), 'application/json') }
else
{ message: Faraday::ParamPart.new(JSON.dump(message), 'application/json') }
end
response = client.post(BASE_URL) do |request|
request.headers['Content-Type'] = 'multipart/form-data'
request.body = payload
end
puts response.status
puts response.body
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment