Home > database >  sending payload for post method in VBA
sending payload for post method in VBA

Time:03-14

I'm trying to login to a website using VBA Excel, but I'm having trouble to send payload post method like in python request. Example in 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

this is what I've tried but fail:

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

the response text shows "Username is required, but was not received", "Password is required, but was not received", "code":400.

Edit:

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)

CodePudding user response:

Turns out that I need to use SetRequestHeader Edit:

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)
  • Related