在 VBA 中为 post 方法发送负载

sending payload for post method in VBA

我正在尝试使用 VBA Excel 登录网站,但我无法像 python 请求中那样发送负载 post 方法。 python

中的示例
def login():
    s = requests.Session()
    payload = {
        'username':sett.username,
        'password':sett.password1
    }
    res = s.post(link, json=payload)
    ref_t = json.loads(res.content)['content']['refresh_token']
    t = json.loads(res.content)['content']['token']
    return t

这是我尝试过但失败的方法:

Dim request As New WinHttpRequest
request.Open "POST", auth_url, False
auth = "username=" & user & "," & "password=" & pass
request.SetRequestHeader "content-type", "application/json;charset=UTF-8"
request.SetCredentials "username:" & user, "password:" & pass, 0
request.Send auth
    
If request.Status <> 200 Then
    MsgBox request.ResponseText
    Exit Sub
End If

响应文本显示“需要用户名,但未收到”,“需要密码,但未收到”,“代码”:400。

编辑:

Dim auth As New Dictionary

auth.Add "username", user
auth.Add "password", pass


request.Open "POST", auth_url, False
request.SetRequestHeader "content-type", "application/json"
request.Send JsonConverter.ConvertToJson(auth)

原来我需要使用SetRequestHeader 编辑:

Dim auth As New Dictionary

auth.Add "username", user
auth.Add "password", pass


request.Open "POST", auth_url, False
request.SetRequestHeader "content-type", "application/json"
request.Send JsonConverter.ConvertToJson(auth)