Making POST/PUT Requests | NodeJS

Learn how to send POST and PUT requests using ScraperAPI via sync, proxy, and async modes. Scrape forms, APIs, and handle raw or form data with full control.

Some advanced users may want to send POST/PUT requests in order to scrape forms and API endpoints directly. You can do this by sending a POST/PUT request through ScraperAPI. The return value will be stringified. So if you want to use it as JSON, you will need to parse it into a JSON object.

  • API ENDPOINT REQUEST

import fetch from 'node-fetch';

// Replace POST with PUT to send a PUT request instead
options = {
  method: 'POST',
  url: 'http://api.scraperapi.com/?api_key=APIKEY&url=http://httpbin.org/post',
  body: JSON.stringify({
    foo: 'bar'
  }),
  headers: {
    'Content-Type': 'application/json',
  },
}

fetch(options)
  .then(response => {
    console.log(response)
  })
  .catch(error => {
    console.log(error)
  })

//For form data
options = {
  method: 'POST',
  url: 'http: //api.scraperapi.com/?api_key=${api_key}&url=http://httpbin.org/post',
    form: {
      foo: 'bar'
    },
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
  },
}

fetch(options)
  .then(response => {
    console.log(response)
  })
  .catch(error => {
    console.log(error)
  })
  • PROXY MODE

const axios = require('axios');

axios.post('http://httpbin.org/ip', 
{
  data: JSON.stringify({foo: 'bar'}),
  headers: {
  'Content-Type': 'application/json',
        },
  proxy: {
    host: 'proxy-server.scraperapi.com',
    port: 8001,
    auth: {
      user: 'scraperapi',
      password: 'APIKEY'  
    },
    protocol: 'http'
  }
})
  .then(response => {
    console.log(response)
  })
  .catch(error => {
    console.log(error)
  });
  • SDK Method

const scraperapiClient = require('scraperapi-sdk')('APIKEY')

options = {
  body: JSON.stringify({foo: 'bar'}),
  headers: {
      'Content-Type': 'application/json',
  }
}

//POST
scraperapiClient.post('http://httpbin.org/anything', options)
.then(response => {
  console.log(response)
})
.catch(error => {
  console.log(error)
})

//PUT
scraperapiClient.put('http://httpbin.org/anything', options)
.then(response => {
  console.log(response)
})
.catch(error => {
  console.log(error)
})
  • ASYNC Method

const fetch = require('node-fetch');

options = {
  method: 'POST',
  url: 'http://async.scraperapi.com/jobs',
  headers: {
    'Content-Type': 'application/json',
  },
  // The data for the target site
  body: JSON.stringify({
    apiKey: 'API_KEY',
    url: 'https://httpbin.org/anything',
    // Replace POST with PUT to send a PUT request instead
    method: 'POST',
    headers: {"content-type": "application/x-www-form-urlencoded"},
    body: "foo=bar"
  }),
}

fetch(options)
  .then(console.log)
  .catch(console.error)    

Last updated

Was this helpful?