dead-brownD
Apify & Crawlee3y ago
1 reply
dead-brown

Error creating new ad-hoc webhook via actor run API call

I am trying to create an Ad-Hoc Webhook when calling the API to start an actor run and I keep getting an error saying
"Webhooks parameter is not a valid JSON: Unexpected token z in JSON at position 0"
.

I'm pretty confident the json is correct, so not sure what to do here.

The code that is being used to submit the Http request (I am using Rails 5.2 with Ruby 2.7.7):
# frozen_string_literal: true

class Scrapers::SimilarWeb::StartRun
  BASE_URL = 'https://api.apify.com/v2/'
  ENDPOINT_URL = 'acts/m0uka~similarweb-scraper/runs'
  RUN_SUCCEEDED_EVENT_TYPE = 'ACTOR.RUN.SUCCEEDED'

  def perform
    start_actor
  end

  private

  def bearer_token
    "Bearer #{Rails.application.credentials.dig(:apify, :token)}"
  end

  def body_json
    {
      proxyConfigurationOptions: {
        'useApifyProxy': true
      },
      websites: websites
    }.to_json
  end

  def headers
    {
      "Content-Type": 'application/json; charset=utf-8',
      "Authorization": bearer_token
    }
  end

  def start_actor
    HTTParty.post(url, body: body_json, headers: headers)
  end

  def url
    "#{BASE_URL}#{ENDPOINT_URL}?webhooks=#{url_encoded_webhooks}"
  end

  def url_encoded_webhooks
    webhooks_json = [{
      'eventTypes': [RUN_SUCCEEDED_EVENT_TYPE],
      'requestUrl': webhook_url
    }].to_json

    URI.encode_www_form_component(webhooks_json)
  end

  def webhook_url
    return [ENV.fetch('WEBHOOK_URL', nil), 'wh/similarweb'].join if Rails.env.development?

    similarweb_webhook_url
  end

  def websites
    Calendar.all.map(&:similarweb_url)
  end
end


Thanks
Was this page helpful?