NodeJS
Ask or search…
K
Links

Making POST/PUT Requests

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)
})
Last modified 4mo ago