Home > front end >  PL/SQL issue with body when making HTTP POST - DVLA API
PL/SQL issue with body when making HTTP POST - DVLA API

Time:08-01

I am working on a project that requires me to use the DVLA's API via PL/SQL. I have the requests working perfectly using Postman, curl and C#. However, I must complete this using PL/SQL. Here is my code:

declare
    v_req utl_http.req;
    v_res utl_http.resp;
    v_url varchar2(4000) := 'https://driver-vehicle-licensing.api.gov.uk/vehicle-enquiry/v1/vehicles';
    v_buffer varchar2(4000); 
    v_content varchar2(4000):= '{ "registrationNumber": "SOME_REG"}';
begin

    utl_http.set_wallet('file:/path/to/wallet', 'Wallet_Password');
    v_req := utl_http.begin_request(v_url, 'POST');
    utl_http.set_header(v_req, 'x-api-key', 'My_API_Key'); 
    utl_http.set_header(v_req, 'content-type', 'application/json'); 

    utl_http.write_text(v_req, v_content);
    v_res := utl_http.get_response(v_req);

      begin
          loop
              utl_http.read_line(v_res, v_buffer);
              dbms_output.put_line(v_buffer);
          end loop;
          utl_http.end_response(v_res);
      exception
          when utl_http.end_of_body 
              then
              utl_http.end_response(v_res);
      end;
  
end;

And the response I am getting back is:

{"errors":[{"status":"400","code":"ENQ103","title":"Bad Request","detail":"Invalid format for field - vehicle registration number"}]}

I have been Googling this for over 8 hours now, and I can't make any progress. Any help greatly appreciated.

CodePudding user response:

Thanks to @AlexPoole, the answer was quite simple. Added the line:

utl_http.set_header(v_req, 'content-length', length(v_content));

and it works perfectly.

  • Related