http post with Bodyrequest Sqlserver

Asked

Viewed 820 times

1

Hello, How do I get a post request by passing an xml as date? tried so on another GET request and it worked:

        DECLARE @Tabela TABLE ( CampoXML XML);

        DECLARE @URL VARCHAR(8000) 
        SELECT @URL = 'url?parametros='
        DECLARE @Response varchar(8000)
        DECLARE @XML xml
        DECLARE @Obj int 
        DECLARE @Result int 
        DECLARE @HTTPStatus int 
        DECLARE @ErrorMsg varchar(MAX)
        EXEC @Result = sp_OACreate 'MSXML2.XMLHttp', @Obj OUT 
        EXEC @Result = sp_OAMethod @Obj, 'open', NULL, 'GET', @URL, false
        EXEC @Result = sp_OAMethod @Obj, 'setRequestHeader', NULL, 'Content-Type', 'application/x-www-form-urlencoded'
        EXEC @Result = sp_OAMethod @Obj, send, NULL, ''
        EXEC @Result = sp_OAGetProperty @Obj, 'status', @HTTPStatus OUT 

        INSERT @Tabela ( CampoXML )
        EXEC @Result = sp_OAGetProperty @Obj, 'responseXML.xml'--, @Response OUT 

        SELECT * FROM @Tabela

but I need to do a post request, and pass body the xml below:

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 <acertaContratoEntrada xmlns="http://...">
   <usuario>usuario</usuario>
   <senha>senha</senha>
 </acertaContratoEntrada>

I appreciate the answers

1 answer

0

I’ll leave the solution here, I was able to make the request POST passing data by body by sqlserver with the code below:

-- Informação a enviar pelo body
DECLARE @BODY_XML VARCHAR(8000) = 
' <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <acertaContratoEntrada xmlns="http://...">
    <usuario>usuario</usuario>
    <senha>senha</senha>
    </acertaContratoEntrada>'


DECLARE @Tabela TABLE ( CampoXML XML);

DECLARE @URL VARCHAR(8000) 
SELECT @URL = 'http://minha-url'
DECLARE @Response varchar(8000)
DECLARE @XML xml
DECLARE @Obj int 
DECLARE @Result int 
DECLARE @HTTPStatus int 
DECLARE @ErrorMsg varchar(MAX)
EXEC @Result = sp_OACreate 'MSXML2.XMLHttp', @Obj OUT 
EXEC @Result = sp_OAMethod @Obj, 'open', NULL, 'POST', @URL, false
EXEC @Result = sp_OAMethod @Obj, 'setRequestHeader', NULL, 'Content-Type', 'application/xml' -- ALTERAÇÃO NECESSARIA PARA FUNCIONAR
EXEC @Result = sp_OAMethod @Obj, send, NULL, @BODY_XML
EXEC @Result = sp_OAGetProperty @Obj, 'status', @HTTPStatus OUT 

-- ================ INSERE NA TABELA TEMPORARIA
INSERT @Tabela ( CampoXML )
EXEC @Result = sp_OAGetProperty @Obj, 'responseXML.xml'--, @Response OUT 

select * from @Tabela

Browser other questions tagged

You are not signed in. Login or sign up in order to post.