VBA: GET and POST method

umamaheswaranp

New Member
Joined
Apr 28, 2016
Messages
2
Hi, I am struck with these two requirements and i couldn't file a solution. I need help please.

* Through VBA, i need to trigger this URL ("https://myserver/connect/login?api_key=xxx").
A successful login on the same redirect URL, i get two parameters; status=success & request_token=yyyy

1. How to capture redirect URL and how to store status and request_token details in a cell.

* After obtaining the success message and request_token from the login flow, i should POST it to this URL(“https://myserver/session”) with these request parameters; request_token=abc, name=xyz, email=xy@xy.com. In response i get the output as JSON.

2. How to send multiple parameters and receive response.

Thanks
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Welcome to the forum! Without an actual site to test, it is hard to help.

Search for this as it will show links with example data: excel winhttp json
 
Upvote 0
You can do this with WinHttpRequest - set a reference to Microsoft WinHTTP Services v5.1.

Something like this to capture the URL redirect:
Code:
    Dim HTTPreq As WinHttpRequest, redirectURL As String
    Set HTTPreq = New WinHttpRequest
    With HTTPreq
        .Open "GET", URL, False   'or POST maybe
        .Option(WinHttpRequestOption_EnableRedirects) = False  'disable automatic redirect
        .setRequestHeader "xxx", "yyy"  'set required request headers
        .Send        
        If .Status = 302 Then  'http redirect code
            redirectURL = .getResponseHeader("Location")   'get redirect URL from Location header
        End If
    End With
You send multiple parameters as form data in a POST request:
Code:
    Dim formData As String
    formData = "request_token=abc&name=xyz&email=" & URLEncode("xy@xy.com")
    With HTTPreq
        .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
        .setRequestHeader "other", "headers"
        .Send (formData) 
        Debug.Print .responseText
    End With
You have to URL encode the parameters and their values, e.g. for the email address because it contains the "@" character. See How can I URL encode a string in Excel VBA? - Stack Overflow.
 
Upvote 0
PS - The second code is missing the POST request:
Code:
With HTTPreq
    .Open "POST", URL, False
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,121
Members
452,381
Latest member
Nova88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top