SQL Server code to POST to an API
source link: https://gist.github.com/theorigin/fa3c58406ff7b4565ca2
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Thanks, dude.
I used it to send a request to Rabbit.
I didn't find any article on how to send the @Body part.
Well done.
Beautiful. Looked everywhere for a code that's working (POST and Body parameters).
I need to send post with authentication bearer token, how to do?
Thanks.
bilobi commented Sep 16, 2022
Hi,
The solution to your question is in the example below.
`DECLARE @object AS INT;
DECLARE @responseText AS VARCHAR(8000);
DECLARE @Body AS varchar(250) =
'{
"username": "bilobi",
"password": "72749764"
}'
EXEC sp_OACreate 'MSXML2.XMLHTTP', @object OUT;
EXEC sp_OAMethod @object, 'open', NULL, 'post','http://localhost/api/authenticate', 'false'
EXEC sp_OAMethod @object, 'setRequestHeader', null, 'Content-Type', 'application/json'
EXEC sp_OAMethod @object, 'send', null, @Body
EXEC sp_OAMethod @object, 'responseText', @responseText OUTPUT
SELECT JSON_VALUE(@responseText,'$.token')
EXEC sp_OADestroy @Object`
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK