Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a REST API from a trigger or stored procedure in mysql?

I want to call rest api in a POST method from a stored procedure or trigger in mysql server on windows.

How do I perform this solely with MySQL?

like image 651
Ashish Avatar asked Nov 07 '16 16:11

Ashish


People also ask

Can we call REST API in stored procedure?

You can use 3rd party API Driver and call REST API in SQL Server like this way (Linked Server + OPENQUERY) - See example API data load in stored proc in below image.

Can MySQL trigger call stored procedure?

MySQL allows you to call a stored procedure from a trigger by using the CALL statement. By doing this, you can reuse the same stored procedure in several triggers. However, the trigger cannot call a stored procedure that has OUT or INOUT parameters or a stored procedure that uses dynamic SQL.

Can I call an API from a SQL query?

Yes, you can use the File System object to open and process files, and you can read and write to files by using the File System object directly within T-SQL.

Can a stored procedure call a trigger?

Trigger: Trigger can't be called from Store Procedure or Function. Store procedure: Stored Procedures can accept any type of parameter. Stored Procedures also accept out parameter. Function: Function can accept any type of parameter.


1 Answers

You can use a mysql-udf-http and then create a trigger like this:

delimiter $$
CREATE TRIGGER upd_check BEFORE UPDATE ON account 
FOR EACH ROW 
  BEGIN 
    IF NEW.amount > 0 THEN 
      set @json = select json_object(account_id,amount) 
      select http_post('http://restservice.example.com/account/post',@json); 
    END IF; 
  END;$$ 

delimiter;
like image 175
perodriguezl Avatar answered Oct 29 '22 21:10

perodriguezl