2

This might seem really silly but I'm trying to get some data from an API (WHMCS). In the docs they have code something like this:

// Call the API 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $whmcsUrl . 'includes/api.php'); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_TIMEOUT, 30); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postfields)); 
$response = curl_exec($ch); 
if (curl_error($ch)) { 
die('Unable to connect: ' . curl_errno($ch) . ' - ' . curl_error($ch)); 
} 
curl_close($ch); 
 
// Decode response 
$jsonData = json_decode($response, true); 
 
// Dump array structure for inspection 
var_dump($jsonData); 

I've written code in nodejs using axios which aims to do the same thing:

axios.get(whmcsUrl + `includes/api.php?accesskey=${apiAccessKey}`, postFields)
.then(res => console.log(res))
.catch(err => console.log(err))

Don't have a deep knowledge of either PHP or Node, please help! I'm getting a 403 Forbidden when I execute this request in node? What am I doing wrong.

Update: Instead of passing an object (postFields), I'm now passing things like username or pw in the url itself:

axios.post(whmcsUrl + `includes/api.php?action=${apiAction}&username=${apiIdentifier}&password=${apiSecret}&accesskey=${apiAccessKey}&responsetype=json`)
.then(res => console.log(res))
.catch(err => console.log(err))

Its still giving me 403 Forbidden error.

2
  • 1
    Are you making a GET or POST? For axios you are doing a GET, but PHP you are doing a POST Commented Aug 19, 2020 at 16:59
  • @AndrewNolan I'm getting the same error in both get and post calls Commented Aug 19, 2020 at 17:01

1 Answer 1

3

The axios equivalent of CURLOPT_POSTFIELDS, http_build_query($postfields) is twofold - first stringify the params and pass them as the post body and secondly specify application/x-www-form-urlencoded as the content type in the headers.

Something like this:

const axios = require('axios')
const qs = require('qs')

const data = qs.stringify(postFieldsObject)
const config = {
  method: 'post',
  url: `${whmcsUrl}includes/api.php`,
  headers: { 
    'Content-Type': 'application/x-www-form-urlencoded'
  },
  data : data
}

axios(config)
.then(function (response) {
  console.log(JSON.stringify(response.data))
})
.catch(function (error) {
  console.log(error)
})
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Can this be done without qs package? I'm not using node anymore.
Yes 'qs.stringify'.You can also build it by hand easily. The key points are that this string is the POST data and not part of the URL and secondly the proper content type header must be passed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.