jsonexcelvbaasp.net-core-webapiwinhttprequest

Assigning Dynamic value within Json


I am trying to do POST request by using "WinHttp.WinHttpRequest.5.1". It works if I send the JSON as shown in #1. However, I want to assign value and make a call with a variable (#2). Is there a way to do that without using third party libs ?

DYNAMIC="Some dynamic value will be here"

'#1 Following works
'body = "{ ""FIRST"": ""dynamic"", ""second"": ""somestring"", ""third"": 100}"

'#2 Folowing call fails.    
body = "{ ""FIRST"": DYNAMIC, ""second"": ""somestring"", ""third"": 100}"

TargetURL = "http://localhost:53518/api/Misc/myMethod/"
Set HTTPReq = CreateObject("WinHttp.WinHttpRequest.5.1")

HTTPReq.SetAutoLogonPolicy 0
HTTPReq.Open "POST", TargetURL, False
HTTPReq.setRequestHeader "Content-Type", "application/json"
HTTPReq.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"
HTTPReq.setRequestHeader "Connection", "keep-alive"
HTTPReq.setRequestHeader "CacheControl", "no-cache"
HTTPReq.setRequestHeader "AcceptLanguage", "en-US,en;q=0.9,ht;q=0.8"
HTTPReq.setRequestHeader "AcceptEncoding", "gzip,deflate,br"
HTTPReq.setRequestHeader "Accept", "*/*"
HTTPReq.setRequestHeader "AcceptCharset", "UTF-8"
HTTPReq.send (body)

Solution

  • Try:

    body = "{ ""FIRST"": " & DYNAMIC & ", ""second"": ""somestring"", ""third"": 100}"
    

    where DYNAMIC = """something""".