Results 1 to 3 of 3
  1. #1
    wideboy is offline Novice
    Windows 10 Access 2016
    Join Date
    Oct 2023
    Posts
    2

    Convert CURL Statement to VBA

    Hi,

    I have a working CURL statement that I need to convert to VBA. The statement is (with sensitive items replaced):

    curl -H "Accept: application/json" -H "x-api-key: MyAPIKey" https://beta.check-mot.service.gov.u...ration=XX99XXX -o "output.txt"

    The VBA code I have so far is:

    Code:
    Public Sub SendHttpPostRequest()
        
        Dim URL, Key, RequestBody As String
        Dim Request As Object
        
        Set Request = CreateObject("WinHttp.WinHttpRequest.5.1")
    
        ' Set the URL for the HTTP POST request
        URL = "https://beta.check-mot.service.gov.uk/trade/vehicles/mot-tests"
        
        ' Set the KEY for the HTTP request with the POST method
        Key = "MyAPIKey"
    
        ' Set the request body (your data to be sent)
        RequestBody = "registration=XX99XXX"
    
        ' Open the HTTP request with the POST method
        Request.Open "POST", URL, False
    
        ' Set the request headers
        Request.setRequestHeader "Content-Type", "application/json"
        Request.setRequestHeader "Accept", "application/json"
        Request.setRequestHeader "Authorization", Key
            
        ' Send the request with the request body
        Request.send 'RequestBody
    
        ' Check for a successful response (status code 200)
        If Request.Status = 200 Then
            Debug.Print "Request successful. Response text: " & Request.responseText
        Else
            Debug.Print "Request failed. Status code: " & Request.Status & ", Response text: " & Request.responseText
        End If
    
    End Sub
    I am an extremely novice programmer and have only gotten this far based on many internet searches and ChatGPT. I feel the error is in the Authorization request but all the service provider says is to use "x-api-key". References for Scripting Runtime, XML and WinHTTP Services are all enabled.

    I get the error "Authorization header requires 'Credential' parameter. Authorization header requires 'Signature' parameter. Authorization header requires 'SignedHeaders' parameter. Authorization header requires existence of either a 'X-Amz-Date' or a 'Date' header. Authorization=MyAPIKey" when I try to run it.



    The output to a file has not been implemented in that code but suggestions welcomed on how to do it.

    Thanks in advance.

  2. #2
    wideboy is offline Novice
    Windows 10 Access 2016
    Join Date
    Oct 2023
    Posts
    2
    I have been able to use a workaround through the Call Shell function to run the curl command from the Command Prompt & only download successful results. Going to leave this here for any person who may need it in the future.

    Code:
    Call Shell("cmd /c" & "curl -s -S -f -H ""Accept: application/json"" -H ""x-api-key: MyAPIKey"" https://beta.check-mot.service.gov.uk/trade/vehicles/mot-tests?registration=" & Reg & " --output ""C:\CNV\Data\JSON\" & Reg & ".json""", vbHide

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Thanks for updating the thread with your solution and welcome to the site! Hopefully we can be more help in the future.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Perform a curl command (HTTP request to a website)
    By bartvanleeuwen55 in forum Programming
    Replies: 2
    Last Post: 07-19-2023, 09:36 AM
  2. Replies: 1
    Last Post: 10-07-2022, 11:10 AM
  3. Replies: 2
    Last Post: 06-25-2021, 03:15 PM
  4. Convert IF statement into Loop Function
    By BGT in forum Programming
    Replies: 6
    Last Post: 04-03-2021, 02:28 AM
  5. Replies: 11
    Last Post: 04-29-2015, 01:38 PM

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