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
# 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
1 Reply
sensitive-blue
sensitive-blueOP2y ago
After some more experimenting, I was able to get the request to submit without error by moving the webhooks value into the body of the post request, instead of in the query params.
def body_json
{
'proxyConfigurationOptions': {
'useApifyProxy': true
},
'webhooks': webhooks,
'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 body_json
{
'proxyConfigurationOptions': {
'useApifyProxy': true
},
'webhooks': webhooks,
'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

Did you find this page helpful?