Rails - How to add contacts to SendGrid marketing campaigns via API

前端 未结 2 1882
野性不改
野性不改 2021-01-24 06:01

I need to make HTTP get and post requests with SendGrid to add contacts to our account, however there doesn\'t seem to be a gem for their email marketing functionality.

2条回答
  •  无人共我
    2021-01-24 06:30

    To add marketing contacts to your SendGrid account via the API, see the documentation at https://sendgrid.api-docs.io/v3.0/contacts-api-recipients/add-recipients

    You can see sample code in the "code generation" section of the page.

    require 'uri'
    require 'net/http'
    
    url = URI("https://api.sendgrid.com/v3/contactdb/recipients")
    
    http = Net::HTTP.new(url.host, url.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    
    request = Net::HTTP::Post.new(url)
    request["authorization"] = 'Bearer <>'
    request["content-type"] = 'application/json'
    request.body = "[{\"email\":\"example@example.com\",\"first_name\":\"\",\"last_name\":\"User\",\"age\":25},{\"email\":\"example2@example.com\",\"first_name\":\"Example\",\"last_name\":\"User\",\"age\":25}]"
    
    response = http.request(request)
    puts response.read_body
    

提交回复
热议问题