1

I have the following PHP/CURL. I need to send it via JS/AJAX (Axios)

How to adapt to JS?

$username = 'test';
$password = 'test';

$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, 'https://example.com/pc_api/index.php/token');
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl_handle, CURLOPT_USERPWD, $username . ':' . $password);
curl_setopt($curl_handle, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
Sven Delueg
  • 1,001
  • 11
  • 23
  • I think this solves your problem: https://stackoverflow.com/questions/5507234/how-to-use-basic-auth-with-jquery-and-ajax – Miguel Costa Jun 22 '17 at 10:49

1 Answers1

3

To use with axios, Use auth for basic authentication :

axios({
    method: 'get',
    url: 'https://example.com/pc_api/index.php/token',
    responseType: 'json', // default is json
    auth: {
        username: 'test',
        password: 'test'
    }
}).then(function(response) {
    console.log(response);
}).catch(function(error) {
    console.log(error);
});

Check request config params

Bertrand Martel
  • 42,756
  • 16
  • 135
  • 159