SQL Server - Call a REST API using a virtual CSV





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I have the code below that makes a request to an API and works fine, using JSON content-type to another endpoint.



DECLARE @Object AS int;
DECLARE @ResponSEText AS Varchar(8000);
DECLARE @Token AS Varchar(8000);
DECLARE @xmltest AS Varchar(8000);
DECLARE @hResult AS int
DECLARE @source varchar(255), @desc varchar(255)
DECLARE @LocationId varchar(25);

DECLARE @Body AS varchar(8000) = '{
"UserName": "test.integration12",
"Password": "Urgent123"
}'

--EXEC sp_OACREATE 'MSXML2.XMLHTTP', @Object OUT;
EXEC sp_OACREATE 'MSXML2.ServerXMLHttp', @Object OUT;
EXEC sp_OAMethod @Object, 'open', NULL, 'POST',
'https://urgentcargus.azure-api.net/api/LoginUser', 'false'
EXEC sp_OAMethod @Object, 'SETRequestHeader', null, 'Content-Type', 'application/json'

DECLARE @len int
SET @len = len(@body)
EXEC sp_OAMethod @Object, 'SETRequestHeader', null, 'Ocp-Apim-Subscription-Key','4f82f9d067914287979884f920d86ffb'
EXEC sp_OAMethod @Object, 'SETRequestHeader', null, 'Ocp-Apim-Trace:true'
EXEC sp_OAMethod @Object, 'SETRequestHeader', null, 'Content-Length', @len
EXEC sp_OAMethod @Object, 'SETRequestBody', null, 'Body', @body
EXEC sp_OAMethod @Object, 'sEND', null, @body
EXEC sp_OAMethod @Object, 'responSEText', @ResponSEText OUTPUT


I would like to adapt the code above in order to send the values of the parameters like a virtual csv file instead of an XML or JSON protocol with the appropriate format to another endpoint using a different content-type and I cannot figure it out how to do this:



DECLARE @Object AS int;
DECLARE @ResponSEText AS Varchar(8000);
DECLARE @Token AS Varchar(8000);
DECLARE @xmltest AS Varchar(8000);
DECLARE @hResult AS int
DECLARE @source varchar(255), @desc varchar(255)
DECLARE @LocationId varchar(25);

DECLARE @Body AS varchar(8000) = -- here should go the parameters which are username, clientid, password and file (where file has to be the virtual CSV file with the columns and rows separated by "|"


EXEC sp_OACREATE 'MSXML2.ServerXMLHttp', @Object OUT;
EXEC sp_OAMethod @Object, 'open', NULL, 'POST',
'https://www.selfawb.ro/import_awb_integrat.php', 'false'
EXEC sp_OAMethod @Object, 'SETRequestHeader', null, 'Content-Type', 'text/xml'

EXEC sp_OAMethod @Object, 'SETRequestHeader', null, 'Ocp-Apim-Trace:true'
EXEC sp_OAMethod @Object, 'SETRequestBody', null, 'Body', @body
EXEC sp_OAMethod @Object, 'sEND', null, @body
EXEC sp_OAMethod @Object, 'responSEText', @ResponSEText OUTPUT


I asked the owner of the API and they provided to me an example as shown in the print screen below:



Example



Does anyone can give me a hint or a hand? Thanks










share|improve this question























  • By "virtual" csv, do you mean replace just the JSON with comma delimited text?

    – Vance McCorkle
    Nov 16 '18 at 19:05











  • yes, this is exactly what I meant. But only that is virtual, I don't use a file already generated.

    – Mihail Codrin
    Nov 16 '18 at 19:18




















1















I have the code below that makes a request to an API and works fine, using JSON content-type to another endpoint.



DECLARE @Object AS int;
DECLARE @ResponSEText AS Varchar(8000);
DECLARE @Token AS Varchar(8000);
DECLARE @xmltest AS Varchar(8000);
DECLARE @hResult AS int
DECLARE @source varchar(255), @desc varchar(255)
DECLARE @LocationId varchar(25);

DECLARE @Body AS varchar(8000) = '{
"UserName": "test.integration12",
"Password": "Urgent123"
}'

--EXEC sp_OACREATE 'MSXML2.XMLHTTP', @Object OUT;
EXEC sp_OACREATE 'MSXML2.ServerXMLHttp', @Object OUT;
EXEC sp_OAMethod @Object, 'open', NULL, 'POST',
'https://urgentcargus.azure-api.net/api/LoginUser', 'false'
EXEC sp_OAMethod @Object, 'SETRequestHeader', null, 'Content-Type', 'application/json'

DECLARE @len int
SET @len = len(@body)
EXEC sp_OAMethod @Object, 'SETRequestHeader', null, 'Ocp-Apim-Subscription-Key','4f82f9d067914287979884f920d86ffb'
EXEC sp_OAMethod @Object, 'SETRequestHeader', null, 'Ocp-Apim-Trace:true'
EXEC sp_OAMethod @Object, 'SETRequestHeader', null, 'Content-Length', @len
EXEC sp_OAMethod @Object, 'SETRequestBody', null, 'Body', @body
EXEC sp_OAMethod @Object, 'sEND', null, @body
EXEC sp_OAMethod @Object, 'responSEText', @ResponSEText OUTPUT


I would like to adapt the code above in order to send the values of the parameters like a virtual csv file instead of an XML or JSON protocol with the appropriate format to another endpoint using a different content-type and I cannot figure it out how to do this:



DECLARE @Object AS int;
DECLARE @ResponSEText AS Varchar(8000);
DECLARE @Token AS Varchar(8000);
DECLARE @xmltest AS Varchar(8000);
DECLARE @hResult AS int
DECLARE @source varchar(255), @desc varchar(255)
DECLARE @LocationId varchar(25);

DECLARE @Body AS varchar(8000) = -- here should go the parameters which are username, clientid, password and file (where file has to be the virtual CSV file with the columns and rows separated by "|"


EXEC sp_OACREATE 'MSXML2.ServerXMLHttp', @Object OUT;
EXEC sp_OAMethod @Object, 'open', NULL, 'POST',
'https://www.selfawb.ro/import_awb_integrat.php', 'false'
EXEC sp_OAMethod @Object, 'SETRequestHeader', null, 'Content-Type', 'text/xml'

EXEC sp_OAMethod @Object, 'SETRequestHeader', null, 'Ocp-Apim-Trace:true'
EXEC sp_OAMethod @Object, 'SETRequestBody', null, 'Body', @body
EXEC sp_OAMethod @Object, 'sEND', null, @body
EXEC sp_OAMethod @Object, 'responSEText', @ResponSEText OUTPUT


I asked the owner of the API and they provided to me an example as shown in the print screen below:



Example



Does anyone can give me a hint or a hand? Thanks










share|improve this question























  • By "virtual" csv, do you mean replace just the JSON with comma delimited text?

    – Vance McCorkle
    Nov 16 '18 at 19:05











  • yes, this is exactly what I meant. But only that is virtual, I don't use a file already generated.

    – Mihail Codrin
    Nov 16 '18 at 19:18
















1












1








1








I have the code below that makes a request to an API and works fine, using JSON content-type to another endpoint.



DECLARE @Object AS int;
DECLARE @ResponSEText AS Varchar(8000);
DECLARE @Token AS Varchar(8000);
DECLARE @xmltest AS Varchar(8000);
DECLARE @hResult AS int
DECLARE @source varchar(255), @desc varchar(255)
DECLARE @LocationId varchar(25);

DECLARE @Body AS varchar(8000) = '{
"UserName": "test.integration12",
"Password": "Urgent123"
}'

--EXEC sp_OACREATE 'MSXML2.XMLHTTP', @Object OUT;
EXEC sp_OACREATE 'MSXML2.ServerXMLHttp', @Object OUT;
EXEC sp_OAMethod @Object, 'open', NULL, 'POST',
'https://urgentcargus.azure-api.net/api/LoginUser', 'false'
EXEC sp_OAMethod @Object, 'SETRequestHeader', null, 'Content-Type', 'application/json'

DECLARE @len int
SET @len = len(@body)
EXEC sp_OAMethod @Object, 'SETRequestHeader', null, 'Ocp-Apim-Subscription-Key','4f82f9d067914287979884f920d86ffb'
EXEC sp_OAMethod @Object, 'SETRequestHeader', null, 'Ocp-Apim-Trace:true'
EXEC sp_OAMethod @Object, 'SETRequestHeader', null, 'Content-Length', @len
EXEC sp_OAMethod @Object, 'SETRequestBody', null, 'Body', @body
EXEC sp_OAMethod @Object, 'sEND', null, @body
EXEC sp_OAMethod @Object, 'responSEText', @ResponSEText OUTPUT


I would like to adapt the code above in order to send the values of the parameters like a virtual csv file instead of an XML or JSON protocol with the appropriate format to another endpoint using a different content-type and I cannot figure it out how to do this:



DECLARE @Object AS int;
DECLARE @ResponSEText AS Varchar(8000);
DECLARE @Token AS Varchar(8000);
DECLARE @xmltest AS Varchar(8000);
DECLARE @hResult AS int
DECLARE @source varchar(255), @desc varchar(255)
DECLARE @LocationId varchar(25);

DECLARE @Body AS varchar(8000) = -- here should go the parameters which are username, clientid, password and file (where file has to be the virtual CSV file with the columns and rows separated by "|"


EXEC sp_OACREATE 'MSXML2.ServerXMLHttp', @Object OUT;
EXEC sp_OAMethod @Object, 'open', NULL, 'POST',
'https://www.selfawb.ro/import_awb_integrat.php', 'false'
EXEC sp_OAMethod @Object, 'SETRequestHeader', null, 'Content-Type', 'text/xml'

EXEC sp_OAMethod @Object, 'SETRequestHeader', null, 'Ocp-Apim-Trace:true'
EXEC sp_OAMethod @Object, 'SETRequestBody', null, 'Body', @body
EXEC sp_OAMethod @Object, 'sEND', null, @body
EXEC sp_OAMethod @Object, 'responSEText', @ResponSEText OUTPUT


I asked the owner of the API and they provided to me an example as shown in the print screen below:



Example



Does anyone can give me a hint or a hand? Thanks










share|improve this question














I have the code below that makes a request to an API and works fine, using JSON content-type to another endpoint.



DECLARE @Object AS int;
DECLARE @ResponSEText AS Varchar(8000);
DECLARE @Token AS Varchar(8000);
DECLARE @xmltest AS Varchar(8000);
DECLARE @hResult AS int
DECLARE @source varchar(255), @desc varchar(255)
DECLARE @LocationId varchar(25);

DECLARE @Body AS varchar(8000) = '{
"UserName": "test.integration12",
"Password": "Urgent123"
}'

--EXEC sp_OACREATE 'MSXML2.XMLHTTP', @Object OUT;
EXEC sp_OACREATE 'MSXML2.ServerXMLHttp', @Object OUT;
EXEC sp_OAMethod @Object, 'open', NULL, 'POST',
'https://urgentcargus.azure-api.net/api/LoginUser', 'false'
EXEC sp_OAMethod @Object, 'SETRequestHeader', null, 'Content-Type', 'application/json'

DECLARE @len int
SET @len = len(@body)
EXEC sp_OAMethod @Object, 'SETRequestHeader', null, 'Ocp-Apim-Subscription-Key','4f82f9d067914287979884f920d86ffb'
EXEC sp_OAMethod @Object, 'SETRequestHeader', null, 'Ocp-Apim-Trace:true'
EXEC sp_OAMethod @Object, 'SETRequestHeader', null, 'Content-Length', @len
EXEC sp_OAMethod @Object, 'SETRequestBody', null, 'Body', @body
EXEC sp_OAMethod @Object, 'sEND', null, @body
EXEC sp_OAMethod @Object, 'responSEText', @ResponSEText OUTPUT


I would like to adapt the code above in order to send the values of the parameters like a virtual csv file instead of an XML or JSON protocol with the appropriate format to another endpoint using a different content-type and I cannot figure it out how to do this:



DECLARE @Object AS int;
DECLARE @ResponSEText AS Varchar(8000);
DECLARE @Token AS Varchar(8000);
DECLARE @xmltest AS Varchar(8000);
DECLARE @hResult AS int
DECLARE @source varchar(255), @desc varchar(255)
DECLARE @LocationId varchar(25);

DECLARE @Body AS varchar(8000) = -- here should go the parameters which are username, clientid, password and file (where file has to be the virtual CSV file with the columns and rows separated by "|"


EXEC sp_OACREATE 'MSXML2.ServerXMLHttp', @Object OUT;
EXEC sp_OAMethod @Object, 'open', NULL, 'POST',
'https://www.selfawb.ro/import_awb_integrat.php', 'false'
EXEC sp_OAMethod @Object, 'SETRequestHeader', null, 'Content-Type', 'text/xml'

EXEC sp_OAMethod @Object, 'SETRequestHeader', null, 'Ocp-Apim-Trace:true'
EXEC sp_OAMethod @Object, 'SETRequestBody', null, 'Body', @body
EXEC sp_OAMethod @Object, 'sEND', null, @body
EXEC sp_OAMethod @Object, 'responSEText', @ResponSEText OUTPUT


I asked the owner of the API and they provided to me an example as shown in the print screen below:



Example



Does anyone can give me a hint or a hand? Thanks







sql-server api






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 17:31









Mihail CodrinMihail Codrin

134




134













  • By "virtual" csv, do you mean replace just the JSON with comma delimited text?

    – Vance McCorkle
    Nov 16 '18 at 19:05











  • yes, this is exactly what I meant. But only that is virtual, I don't use a file already generated.

    – Mihail Codrin
    Nov 16 '18 at 19:18





















  • By "virtual" csv, do you mean replace just the JSON with comma delimited text?

    – Vance McCorkle
    Nov 16 '18 at 19:05











  • yes, this is exactly what I meant. But only that is virtual, I don't use a file already generated.

    – Mihail Codrin
    Nov 16 '18 at 19:18



















By "virtual" csv, do you mean replace just the JSON with comma delimited text?

– Vance McCorkle
Nov 16 '18 at 19:05





By "virtual" csv, do you mean replace just the JSON with comma delimited text?

– Vance McCorkle
Nov 16 '18 at 19:05













yes, this is exactly what I meant. But only that is virtual, I don't use a file already generated.

– Mihail Codrin
Nov 16 '18 at 19:18







yes, this is exactly what I meant. But only that is virtual, I don't use a file already generated.

– Mihail Codrin
Nov 16 '18 at 19:18














1 Answer
1






active

oldest

votes


















0














If you want to send a virtual file, e.g. a file that you generate in memory then send as an attachment as part of the POST, then you would just need include the csv text as part of the post as an attachment with a filename. Try using PostMan to set this up using an actual csv file and analyze the POST to view how to format the csv text to be serialized over the wire as a csv file attachment.



Otherwise if you are using SQL 2016 or above, you can parse csv text using STRING_SPLIT. Hope this helps.



DECLARE @CsvParams NVARCHAR(256) = 
'UserName,test.integration12,'
+ 'Password,Urgent123'

SELECT value FROM STRING_SPLIT(@CsvParams, ',');


The code above produces a table with the separated values:



enter image description here



More on STRING_SPLIT here:
STRING_SPLIT (Transact-SQL)






share|improve this answer


























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53342740%2fsql-server-call-a-rest-api-using-a-virtual-csv%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    If you want to send a virtual file, e.g. a file that you generate in memory then send as an attachment as part of the POST, then you would just need include the csv text as part of the post as an attachment with a filename. Try using PostMan to set this up using an actual csv file and analyze the POST to view how to format the csv text to be serialized over the wire as a csv file attachment.



    Otherwise if you are using SQL 2016 or above, you can parse csv text using STRING_SPLIT. Hope this helps.



    DECLARE @CsvParams NVARCHAR(256) = 
    'UserName,test.integration12,'
    + 'Password,Urgent123'

    SELECT value FROM STRING_SPLIT(@CsvParams, ',');


    The code above produces a table with the separated values:



    enter image description here



    More on STRING_SPLIT here:
    STRING_SPLIT (Transact-SQL)






    share|improve this answer






























      0














      If you want to send a virtual file, e.g. a file that you generate in memory then send as an attachment as part of the POST, then you would just need include the csv text as part of the post as an attachment with a filename. Try using PostMan to set this up using an actual csv file and analyze the POST to view how to format the csv text to be serialized over the wire as a csv file attachment.



      Otherwise if you are using SQL 2016 or above, you can parse csv text using STRING_SPLIT. Hope this helps.



      DECLARE @CsvParams NVARCHAR(256) = 
      'UserName,test.integration12,'
      + 'Password,Urgent123'

      SELECT value FROM STRING_SPLIT(@CsvParams, ',');


      The code above produces a table with the separated values:



      enter image description here



      More on STRING_SPLIT here:
      STRING_SPLIT (Transact-SQL)






      share|improve this answer




























        0












        0








        0







        If you want to send a virtual file, e.g. a file that you generate in memory then send as an attachment as part of the POST, then you would just need include the csv text as part of the post as an attachment with a filename. Try using PostMan to set this up using an actual csv file and analyze the POST to view how to format the csv text to be serialized over the wire as a csv file attachment.



        Otherwise if you are using SQL 2016 or above, you can parse csv text using STRING_SPLIT. Hope this helps.



        DECLARE @CsvParams NVARCHAR(256) = 
        'UserName,test.integration12,'
        + 'Password,Urgent123'

        SELECT value FROM STRING_SPLIT(@CsvParams, ',');


        The code above produces a table with the separated values:



        enter image description here



        More on STRING_SPLIT here:
        STRING_SPLIT (Transact-SQL)






        share|improve this answer















        If you want to send a virtual file, e.g. a file that you generate in memory then send as an attachment as part of the POST, then you would just need include the csv text as part of the post as an attachment with a filename. Try using PostMan to set this up using an actual csv file and analyze the POST to view how to format the csv text to be serialized over the wire as a csv file attachment.



        Otherwise if you are using SQL 2016 or above, you can parse csv text using STRING_SPLIT. Hope this helps.



        DECLARE @CsvParams NVARCHAR(256) = 
        'UserName,test.integration12,'
        + 'Password,Urgent123'

        SELECT value FROM STRING_SPLIT(@CsvParams, ',');


        The code above produces a table with the separated values:



        enter image description here



        More on STRING_SPLIT here:
        STRING_SPLIT (Transact-SQL)







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 20 '18 at 2:39

























        answered Nov 20 '18 at 2:12









        Vance McCorkleVance McCorkle

        41645




        41645
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53342740%2fsql-server-call-a-rest-api-using-a-virtual-csv%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python