Search code examples
ajaxweb-scrapingapify

How to override Apify's actor input configuration by passing a JSON object as the POST payload?


Trying to override the Apify's Google Scraper actor's queries by passing the data object as given below. I am getting 400 and and 403 error message. When I remove the data playload, it works fine. It then returns the result with the default queries.

1) What is the right way to pass the playload to override the queries parameters.

2) How can I send multiple search queries like "link building", "link building service" ?

$.ajax({
      url : 'https://api.apify.com/v2/actor-tasks/XXXXXXX/runs?token=XXXXXXXX&waitForFinish=120,  
      method : 'POST',
      contentType: 'application/json',
       data : {   // when I add this I get 400 error.
          "queries" : "Outreach link building",
        },
       success:function(result) {
            console.log(result);
      } 

});

Thanks in advance.


Solution

  • 1) You need to stringified JSON and use proper dataType:

    $.ajax({
          url : 'https://api.apify.com/v2/actor-tasks/XXXXXXX/runs?token=XXXXXXXX&waitForFinish=120',  
          method : 'POST',
          contentType: 'application/json',
          dataType: 'json',
          data : JSON.stringify ({
              "queries" : "Outreach link building"
          }),
          success:function(result) {
            console.log(result);
          } 
    });
    

    You can read about that in this post.

    2) If you want to send multiple queries you need to separate them using the new line:

    {
      "queries": "Outreach link building\nquery"
    }