Im trying to send a post request using vba and struggling to convert my python code to vba. In python I can do this
import requests
headers = {
"authority": "platform.example.com",
"accept": "application/json, text/plain, */*",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36",
"content-type": "application/json",
"sec-fetch-site": "same-site",
"sec-fetch-mode": "cors",
"sec-fetch-dest": "empty",
"accept-language": "en-US,en;q=0.9",
}
data = "{"messages":[{"to":"12345","channel":"sms","content":"Test message"}]}"
r=requests.post("https://example.com/v1/message", headers=headers, data=data)
I have tried this in vba but no luck. Msgbox comes up blank
Sub test()
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
Url = "https://example.com/v1/message"
objHTTP.Open "POST", Url, False
objHTTP.setRequestHeader "authority", "platform.example.com",
objHTTP.setRequestHeader "accept", "application/json, text/plain, */*"
objHTTP.setRequestHeader "user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36"
objHTTP.setRequestHeader "content-type", "application/json"
objHTTP.setRequestHeader "sec-fetch-site", "same-site"
objHTTP.setRequestHeader "sec-fetch-mode", "cors"
objHTTP.setRequestHeader "sec-fetch-dest", "empty"
objHTTP.setRequestHeader "accept-language", "en-US,en;q=0.9"
Data = """messages=[{'to':'1234','channel':'sms','content':'Test message'}]"""
objHTTP.send Data
MsgBox objHTTP.ResponseText
End Sub
How can I get the vba working?
CodePudding user response:
Try with double-quotes to replicate the Data
that works:
Data = "{""messages"":[{""to"":""1234"",""channel"":""sms"",""content"":""Test message""}]}"