SQL Server
SQL Server
curl with Variable Substitution in a GraphQL Request Body
See more CURL Examples
This example shows how to use variables inside agraphql request body using the {{variable_name}} syntax. When the HTTP request’s Content-Type indicates graphql, Chilkat automatically applies proper escaping to each substituted value, ensuring the resulting graphql remains valid.
Chilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
SELECT @success = 0
-- Variable names are enclosed between {{ and }}
-- Important: Variables should be placed inside the quotes.
-- curl -X POST https://api.example.com/graphql \
-- -H "Content-Type: application/graphql; charset=utf-8" \
-- -H "Accept: application/json" \
-- --data-binary "mutation {
-- createUser(
-- input: {
-- name: \"{{name}}\"
-- city: \"{{city}}\"
-- note: \"{{note}}\"
-- bio: \"{{bio}}\"
-- }
-- ) {
-- id
-- name
-- }
-- }"
-- Build the above curl command.
DECLARE @sbCurl int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbCurl OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OAMethod @sbCurl, 'AppendLn', @success OUT, 'curl -X POST https://api.example.com/graphql \'
EXEC sp_OAMethod @sbCurl, 'AppendLn', @success OUT, ' -H "Content-Type: application/graphql; charset=utf-8" \'
EXEC sp_OAMethod @sbCurl, 'AppendLn', @success OUT, ' -H "Accept: application/json" \'
EXEC sp_OAMethod @sbCurl, 'AppendLn', @success OUT, ' -d "mutation {'
EXEC sp_OAMethod @sbCurl, 'AppendLn', @success OUT, ' createUser('
EXEC sp_OAMethod @sbCurl, 'AppendLn', @success OUT, ' input: {'
EXEC sp_OAMethod @sbCurl, 'AppendLn', @success OUT, ' name: \"{{name}}\"'
EXEC sp_OAMethod @sbCurl, 'AppendLn', @success OUT, ' city: \"{{city}}\"'
EXEC sp_OAMethod @sbCurl, 'AppendLn', @success OUT, ' note: \"{{note}}\"'
EXEC sp_OAMethod @sbCurl, 'AppendLn', @success OUT, ' bio: \"{{bio}}\"'
EXEC sp_OAMethod @sbCurl, 'AppendLn', @success OUT, ' }'
EXEC sp_OAMethod @sbCurl, 'AppendLn', @success OUT, ' ) {'
EXEC sp_OAMethod @sbCurl, 'AppendLn', @success OUT, ' id'
EXEC sp_OAMethod @sbCurl, 'AppendLn', @success OUT, ' name'
EXEC sp_OAMethod @sbCurl, 'AppendLn', @success OUT, ' }'
EXEC sp_OAMethod @sbCurl, 'AppendLn', @success OUT, '}"'
DECLARE @curl int
EXEC @hr = sp_OACreate 'Chilkat.HttpCurl', @curl OUT
-- Provide values for variables
EXEC sp_OAMethod @curl, 'SetVar', NULL, 'name', 'José O''Connor'
EXEC sp_OAMethod @curl, 'SetVar', NULL, 'city', 'München'
EXEC sp_OAMethod @curl, 'SetVar', NULL, 'note', 'He said "Hello, world!" — and left…'
EXEC sp_OAMethod @curl, 'SetVar', NULL, 'bio', 'Loves sushi, café visits, and π ≈ 3.14159'
-- To demonstrate how the variables are replaced, this example does not execute the curl command.
-- Instead, it generates the raw HTTP request that would be sent if the curl command were run.
DECLARE @sbRawRequest int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbRawRequest OUT
EXEC sp_OAMethod @sbCurl, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @curl, 'ToRawRequest', @success OUT, @sTmp0, @sbRawRequest
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @curl, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @sbCurl
EXEC @hr = sp_OADestroy @curl
EXEC @hr = sp_OADestroy @sbRawRequest
RETURN
END
EXEC sp_OAMethod @sbRawRequest, 'GetAsString', @sTmp0 OUT
PRINT @sTmp0
-- The output is shown below.
-- Notice the quote chars around "Hello, world!" are properly escaped.
-- POST /graphql HTTP/1.1
-- Accept: application/json
-- Host: api.example.com
-- Content-Type: application/graphql; charset=utf-8
-- Content-Length: 250
--
-- mutation {
-- createUser(
-- input: {
-- name: "José O'Connor"
-- city: "München"
-- note: "He said \"Hello, world!\" — and left…"
-- bio: "Loves sushi, café visits, and π ≈ 3.14159"
-- }
-- ) {
-- id
-- name
-- }
-- }
EXEC @hr = sp_OADestroy @sbCurl
EXEC @hr = sp_OADestroy @curl
EXEC @hr = sp_OADestroy @sbRawRequest
END
GO