We help IT Professionals succeed at work.
Get Started

http 407 status error with SQL stored proc

440 Views
Last Modified: 2016-07-05
Hello!

I am trying to complete a HTTP post request via a stored proc.

The query returns the error: sp_OAMethod http status  407 which leads me to think proxy issues and that the request is never making it to the server.  The prints are in there for troubleshooting and it seems to break after step 5.

Any help would be greatly appreciated


Thanks!

CODE---------------------
USE [db]
GO
/****** Object:  StoredProcedure [dbo].[HTTP_POST]   ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[HTTP_POST]( @sUrl varchar(8000)='https://api.apiurl/oauth/token?grant_type=refresh_token&client_id=
xxxxxxx&client_secret=XXXX-XXXX&refresh_token=XXXXXXX', @response varchar(8000)
out)
As


Declare
@obj int
,@hr int
,@status int
,@msg varchar(255)
print @msg


exec @hr = sp_OACreate 'MSXML2.ServerXMLHttp', @obj OUT
if @hr <> 0 begin Raiserror('sp_OACreate MSXML2.ServerXMLHttp.3.0
failed', 16,1) return end

print '1'

exec @hr = sp_OAMethod @obj, 'open', NULL, 'POST', @sUrl, false
if @hr <>0 begin set @msg = 'sp_OAMethod Open failed' goto eh end


print '2'

exec @hr = sp_OAMethod @obj, 'setRequestHeader', NULL, 'Content-Type',
'application/x-www-form-urlencoded'
if @hr <>0 begin set @msg = 'sp_OAMethod setRequestHeader failed' goto
eh end


print '3'

exec @hr = sp_OAMethod @obj, send, NULL, ''
if @hr <>0 begin set @msg = 'sp_OAMethod Send failed' goto eh end

print '4'

exec @hr = sp_OAGetProperty @obj, 'status', @status OUT
if @hr <>0 begin set @msg = 'sp_OAMethod read status failed' goto
eh
end

print '5'

if @status <> 200 begin set @msg = 'sp_OAMethod http status ' +
str(@status) goto eh end

print '6'


exec @hr = sp_OAGetProperty @obj, 'responseText', @response OUT
if @hr <>0 begin set @msg = 'sp_OAMethod read response failed' goto
eh end

print '7'

exec @hr = sp_OADestroy @obj
return
eh:
exec @hr = sp_OADestroy @obj
Raiserror(@msg, 16, 1)
return
print '8'







USE [db]
GO

DECLARE      @return_value int,
            @response varchar(8000)

EXEC      @return_value = [dbo].[HTTP_POST]
            @response = @response OUTPUT

SELECT      @response as N'@response'

SELECT      'Return Value' = @return_value

GO
Comment
Watch Question
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE