0

Attempting to send a post request to the DVLA EPS Api using Axios. Both methods I used returned errors.

I first tried how I thought the request should go, then copied the example from the documentation, both didn't work with two different errors.

My codes error: Access to XMLHttpRequest at 'https://uat.driver-vehicle-licensing.api.gov.uk/vehicle-enquiry/v1/vehicles' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Documentation example error: Uncaught TypeError: axios is not a function

My code:

import './App.css';
import axios from 'axios';

function App() {
  var target = JSON.stringify({ registrationNumber: 'AA19AAA' });
  const api_url = "https://uat.driver-vehicle-licensing.api.gov.uk/vehicle-enquiry/v1/vehicles";

  function getData() {
    axios.post(api_url, {
      headers: {
        'x-api-key': 'API KEY HERE',
        'Content-Type': 'application/json',
      },
      data: target
    })
      .then(res => {
        console.log(res.data.content);
      })
      .catch(err => {
        console.log(err);
      })
  }

  return (
    <div className="App">
      <button onClick={getData}>Get Info</button>
    </div>
  );
}

export default App;

Documentation example:

import './App.css';
import axios from 'axios';

function App() {
    var axios = require('axios');

    var data = JSON.stringify({ registrationNumber: 'AA19AAA' });
    
    var config = {
      method: 'post',
      url:
        'https://uat.driver-vehicle-licensing.api.gov.uk/vehicle-enquiry/v1/vehicles',
      headers: {
        'x-api-key': 'API KEY HERE',
        'Content-Type': 'application/json',
      },
      data: data,
    };
    
    axios(config)
      .then(function(response) {
        console.log(JSON.stringify(response.data));
      })
      .catch(function(error) {
        console.log(error);
      });
    
  return (
    <div className="App">
    </div>
  );
}

export default App;

Im completely new to this, any help would be appreciated.

Papgooner
  • 13
  • 5
  • Your code is correct. The "documentation" example is either mistaken or you made a mistake when copying it. `axios` is indeed not a function. It's an object which *has* functions, like the `post` function that you use in your code. – David May 25 '23 at 17:49
  • 1
    Does this answer your question? [No 'Access-Control-Allow-Origin' header is present on the requested resource—when trying to get data from a REST API](https://stackoverflow.com/questions/43871637/no-access-control-allow-origin-header-is-present-on-the-requested-resource-whe) – David May 25 '23 at 17:52
  • @David thanks for the response, it certainly looks like what im aiming for, theres alot to read through so Ill have a go tomorrow. The documentation was here: https://developer-portal.driver-vehicle-licensing.api.gov.uk/apis/vehicle-enquiry-service/code-examples.html#nodejs-using-axios – Papgooner May 25 '23 at 17:56

0 Answers0