我希望能够将json消息发布到REST服务。我可以使用google-search
中的示例进行GET调用Declare @Object as Int;
Declare @ResponseText as Varchar(8000);
Exec sp_OACreate 'MSXML2.XMLHTTP', @Object OUT;
Exec sp_OAMethod @Object, 'open', NULL, 'get',
'http://www.webservicex.com/stockquote.asmx/GetQuote?symbol=MSFT',
--Your Web Service Url (invoked) 'false'
Exec sp_OAMethod @Object, 'send'
Exec sp_OAMethod @Object, 'responseText', @ResponseText OUTPUT
Select @ResponseText
Exec sp_OADestroy @Object
我尝试了以下不同的变体:
Declare @Object as Int;
Declare @ResponseText as Varchar(8000);
DECLARE @hResult int
DECLARE @source varchar(255), @desc varchar(255)
declare @Body as varchar(8000) =
'{
"Subsystem": 1,
"Exception": "",
"Message": "I have done what you asked",
"Time": "2014-06-09T11:16:35",
"Attribute": { "Number of attempts": "0" }
}'
Exec sp_OACreate 'MSXML2.XMLHTTP', @Object OUT;
EXEC sp_OAMethod @Object, 'open', NULL, 'post',
'https://thecorrecturl:446/api/handelse/', 'false'
Exec sp_OAMethod @Object, 'setRequestHeader', null, 'Content-Type', 'application/json'
declare @len int
set @len = len(@body)
EXEC sp_OAMethod @Object, 'setRequestHeader', null, 'Content-Length', @len
Exec sp_OAMethod @Object, 'setRequestBody', null, 'Body', @body
EXEC sp_OAMethod @Object, 'send', null
Exec sp_OAMethod @Object, 'responseText', @ResponseText OUTPUT
Select @ResponseText
Exec sp_OADestroy @Object
我回来的ResponseText是有意义的“{”消息“:”发生错误。“}”
任何人都可以指出我正确的方向来做这件事,或者只是给出正确的方法来做到这一点;)
我已经使用GET调用我的网址并从服务中获得了正确的handelse。
此致 卡罗琳
答案 0 :(得分:10)
Caroline的解决方案:
我的一位同事偶然发现了解决方案
而不是使用
Exec sp_OACreate 'MSXML2.XMLHTTP', @Object OUT;
我们使用了
Exec sp_OACreate 'MSXML2.ServerXMLHTTP', @Object OUT;
将handelse发布到我们服务的最终结果如下
Declare @Object as Int;
Declare @ResponseText as Varchar(8000);
Declare @Body as varchar(8000) =
'{
"Subsystem": 1,
"Exception": "",
"Message": "I have done what you asked",
"Time": "2014-06-09T11:16:35",
"Attribute": { "Number of attempts": "0" }
}'
Exec sp_OACreate 'MSXML2.ServerXMLHTTP', @Object OUT;
EXEC sp_OAMethod @Object, 'open', NULL, 'post','https://thecorrecturl:446/api/handelse/', '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 @ResponseText
Exec sp_OADestroy @Object
希望这有助于其他人
此致 卡罗琳
答案 1 :(得分:0)
Brian查询工作正常。 我想让API密钥添加以下行
declare @apikey as nvarchar(32) = 'your api key';
..
/*after content-type*/
exec sp_OAMethod @object, 'setRequestHeader', null, 'ApiKey', @apikey
...