Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2023
    Posts
    1

    Perform a curl command (HTTP request to a website)

    Hi


    I have a simple curl-command to post data to a Wordpress website.
    The website respons OKE when the command is executed using the Postman-application.

    But no way I can get it running in Visual Basic.

    The curl command is:
    curl --user "useradmin:userspassword"\
    -X POST \
    -H "Content-Type: application/json" \
    -d '{"username":"jane","password":"secret","email":"j ane@example.com","first_name":"jan01","last_name": "jansen01"}' \
    http://www.lrc100test.nl/wp-json/wp/v2/users

  2. #2
    Edgar is online now Competent Performer
    Windows 8 Access 2016
    Join Date
    Dec 2022
    Posts
    274
    Try this:
    Code:
    Sub SendHttpRequest()
        Dim url As String
        Dim httpRequest As Object
        
        url = "http://www.lrc100test.nl/wp-json/wp/v2/users"
        
        Set httpRequest = CreateObject("MSXML2.XMLHTTP")
        
        With httpRequest
            .Open "POST", url, False
            .SetRequestHeader "Authorization", "Basic " & Base64Encode("useradmin:userspassword")
            .SetRequestHeader "Content-Type", "application/json"
            .send "{""username"":""jane"",""password"":""secret"",""email"":""jane@example.com"",""first_name"":""jan01"",""last_name"":""jansen01""}"
            While .ReadyState <> 4
                DoEvents
            Wend
            MsgBox .responseText
        End With
        
        Set httpRequest = Nothing
    End Sub
    I leave the Base64Encode function on your own. When you use --user you need to encode the string "useradmin:userspassword"

    Postman must have it built in, but you have to craft your own base64encode function.

  3. #3
    RoyM is offline Novice
    Windows 10 Office 365
    Join Date
    Jul 2023
    Posts
    1
    My problem is similar. I am trying to convert a cURL commands to VBA. This is the first GET that initiates retrieving up to 200 records in JSON format.


    Code:
    cURL.exe --request GET --Url 'mywebaddress' --user 'myemailaddress.com:mytoken' --header 'Accept: application/json'

    The following returns a status 403 Unauthorized. I haven't been able to figure out how to get it to work.
    Code:
    Dim objHTTP As Object
    Dim sUrl As String
    Dim sStatus as String
    sUrl = "mywebaddress"
    Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
    With objHTTP
    	.Open "GET", sUrl, False
    	.SetCredentials "myemailaddress", "mytoken", 0
    	.setRequestHeader "Accept", "application/json"
    	.send
    End With
    sStatus = objHTTP.status

    Then a GET is executed in loop to retrieve all the rest of the data, 200 records at a time. The only difference is adding a couple options to the web address.


    Lastly, I execute the DELETE cURL against each user to uncheck them from being active users.


    Code:
    cURL.exe --request DELETE --url 'myotherwebaddress' --user 'myemailaddress:mytoken' --header 'Accept: application/json'

    I've spent many hours googling and trying to find the solution. Hopefully someone can help me. Thanks

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 06-25-2021, 03:15 PM
  2. Replies: 1
    Last Post: 12-02-2020, 10:46 AM
  3. Replies: 0
    Last Post: 12-06-2016, 11:28 AM
  4. Replies: 7
    Last Post: 10-09-2014, 03:06 PM
  5. Http
    By student123xyz in forum Access
    Replies: 0
    Last Post: 09-03-2009, 08:59 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums