VBA http Post Request - Postman key/value/body provided

bensko

Board Regular
Joined
Mar 4, 2008
Messages
173
Hi All, I have been working on this for days without success. Even downloaded VBA-WEB and looked at examples there, online and everywhere. I can't seem to get this figured out and I have been working with VBA code for 15 years.

I am pretty new at API but Postman makes things pretty easy. I POST 30-40 schedule work orders a day into Tsheets. Use it do add staff, GET logs to run margin reports, and a variety of other tasks.

I also am able, through Postman, to send a text message to my customers through our SendHub Voip service (see code below). What I am trying to do now though is send a survey request text (with link) to 30-40 clients at the end of each day. Since the message content will include a different web link for each staff member (they each have their own quality survey Google form) I can't bulk send to multiple phone numbers in Postman. Each message has to be unique so I am going to loop through my schedule worksheet, collect phone numbers ,date, and form link, on a "Survey" sheet, then call the Http POST request code to send them one at a time.

http://apidocs.sendhub.com/

the data below is what the postman code looks like to POST to SendHub. I can PM someone my username and Key if needed to test.

POST /v1/messages/?username=847380XXXX&api_key=8f467647fd4a96a1345d1e45192381bc19XXXXXX
HTTP/1.1
Host: api.sendhub.com
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: 38caba79-adfb-4e9b-b2ed-2cf72XXXXXX

Code:
{
   "contacts": [
     "+17085257848"
   ],
   "text": "Please take two seconds to rate your 10/20/18 service: https://goo.gl/forms/***4WSEGSfib6KJG3
}
Here is the code to cycle through the schedule:
Code:
Sub Surveys_Monday()
Range("C4").Select
    GoTo 14
12
    ActiveCell.Offset(2, 0).Select
    If Range("BE" & ActiveCell.Row).Value = 10 Then
    ActiveCell.Offset(-8, 2).Select
    End If
    If ActiveCell.Column = 53 Then
    GoTo 20
    End If
14
    If ActiveCell.Value = "" Or Left(ActiveCell.Value, 1) = ">" Then
    GoTo 12
    Else
    End If
    
    Dim Sh1 As Worksheet, Sh2 As Worksheet
    Set Sh1 = Sheets("Survey")
    Set Sh2 = Sheets("Schedule")
    Sh1.Range("C2").Value = ActiveCell.Value
    Sh1.Range("D2").Value = Sh2.Cells(2, ActiveCell.Column).Value
    Sh1.Range("E2").Value = Range("BA" & ActiveCell.Row).Value
    Sh1.Select
     
    Send_To_SendHub (see below) '''''''API CODE TO TEXT TO CLIENT ''''''''
    
GoTo 12
    
20

End Sub
'''' I have no idea if below is the right code to use, found it online somewhere and it seems to be pretty close to what I need with my Key, Header, etc.

Code:
Sub Send_To_SendHub()
Dim sURL As String, sHTML As String, sAllPosts As String
Dim oHttp As Object
Dim blWSExists As Boolean
Dim Body As String

''''Not sure how to code the body below.  The Date would be located  on the "Survey" sheet in E2, Phone number in F2, and Form Link in G2. Other  text can be placed in H2, I2, etc. or Kept in the code.

Body =  
{    "contacts": [
     "+17085257848"
   ],
   "text": "Please take two seconds to rate your 10/20/18 service:  https://goo.gl/forms/***4WSEGSfib6KJG3
}

Set oHttp = CreateObject("MSXML2.XMLHTTP")
sURL = "https://api.sendhub.com/v1/messages/?username=847380XXXX&api_key=8f467647fd4a96a1345d1e45192381bc19XXXXXX"

oHttp.Open "POST", sURL, False
oHttp.setRequestHeader "Content-type", "application/json"
oHttp.setRequestHeader "Accept", "application/json"
Http.Send Body
sHTML = oHttp.ResponseText
Worksheets("Response").Range("A1").Value = sHTML  'will write additional code to clean this up into a list of surveys sent. 
End Sub
I sincerely appreciate any guidance that can be provieded.

Ben
 

Forum statistics

Threads
1,078,213
Messages
5,338,891
Members
399,265
Latest member
aj17x55

Some videos you may like

This Week's Hot Topics

Top