-
Jan 22nd, 2019, 09:18 AM
#1
Thread Starter
New Member
[RESOLVED] WinHTTP and https API
Hi,
I have a large legacy program written in VB6, Very successfull,
works fine on all flavours of Windows up to and including 10
Now I need to communicate with a third party using HTML Basic Auth and
JSON.
SO far I have created some short code to test the communications:
#
Dim HttpReq As New WinHttp.WinHttpRequest
WITH HttpReq
.SetCredentials "<username>", "<password>", 0
.Open "PUT", "https://<API URL>", False
.Option(WinHttpRequestOption_SecureProtocols) = SecureProtocol_ALL
.Option(WinHttpRequestOption_EnableRedirects) = True
HttpReq.setRequestHeader "Content-Type", "application/json"
.Send " "
.Send "GET https://<API URL>"
#
Both the Send methods result in:
{
"status": "error",
"code": 401,
"message": "Access denied"
}
The sane credentials do work OK in Postman app.
What am I missing or doing wrong please?
-
Jan 22nd, 2019, 10:44 AM
#2
Re: WinHTTP and https API
SetCredentials should be called after Open.
Since you are not sending a JSON payload (indeed, you are doing a GET so sending no payload at all) you don't have to set a Content-Type header.
-
Jan 22nd, 2019, 12:00 PM
#3
Thread Starter
New Member
Re: WinHTTP and https API
I tried that and still get Access Denied.
The whole code will be sending a JSON payload, once I get past this first sumbling block.
-
Jan 22nd, 2019, 12:01 PM
#4
Thread Starter
New Member
Re: WinHTTP and https API
I tried that and still get Access Denied.
The whole code will be sending a JSON payload, once I get past this first sumbling block.
-
Jan 22nd, 2019, 12:50 PM
#5
Thread Starter
New Member
Re: WinHTTP and https API
Solved it!
it is the authorisation header, should be
.setRequestHeader "Authorization", "Basic <Base64 encoded username & password> "
That gets a status 200 "Ok" response
-
Jan 22nd, 2019, 03:48 PM
#6
Re: [RESOLVED] WinHTTP and https API
You can do that, but it's a bit of a hack. If the server ever changes to require NTLM, Kerberos, Digest, etc. instead of Basic it all falls over.
In any case just doing this as documented works just fine. This example fetches a GIF and displays it as the Form's Picture:
Code:
Const HTTPREQUEST_SETCREDENTIALS_FOR_SERVER As Long = 0
Dim Vector As WIA.Vector
With New WinHttp.WinHttpRequest
.Open "GET", GIF_URL, False
.Option(WinHttpRequestOption_SecureProtocols) = SecureProtocol_ALL
.Option(WinHttpRequestOption_EnableRedirects) = True
.SetCredentials USER, PW, HTTPREQUEST_SETCREDENTIALS_FOR_SERVER
.Send
If 200 <= .Status And .Status < 300 Then
Set Vector = New WIA.Vector
Vector.BinaryData = .ResponseBody
Set Picture = Vector.Picture
Else
MsgBox "Status " & CStr(.Status) & vbNewLine & vbNewLine _
& .StatusText
End If
End With
-
Jan 24th, 2019, 09:12 AM
#7
Thread Starter
New Member
Re: [RESOLVED] WinHTTP and https API
Nope, tried that, back to square on with
401
Request Denied
-
Jan 24th, 2019, 09:16 AM
#8
Thread Starter
New Member
Re: [RESOLVED] WinHTTP and https API
Nope, tried that, back to square on with
401
Request Denied
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|