API using Clarion's HttpWebRequest

Hello.
I need to access an API using Clarion’s HttpWebRequest.
Access will only be via GET.
The authentication is “Basic Auth” with a username and password, how do I authenticate with HttpWebRequest?

This is ChatGPT’s response



PROGRAM

  INCLUDE('ClaRunExt.INC'),ONCE

HttpReq             ClaRunExtClass
httpWebAddress      CSTRING(512)
httpVerbMethod      BYTE
postData            CSTRING(1024)
requestParameters   CSTRING(512)
contentType         CSTRING(50)
headers             CSTRING(512)
responseOut         CSTRING(64000)

UserName            CSTRING(50)
Password            CSTRING(50)
Credentials         CSTRING(256)
EncodedCreds        CSTRING(512)
ReturnValue         LONG

  CODE
  
  ! Your credentials
  UserName = 'myusername'
  Password = 'mypassword'
  
  ! Create credentials string (username:password)
  Credentials = CLIP(UserName) & ':' & CLIP(Password)
  
  ! Encode to Base64
  EncodedCreds = Base64Encode(Credentials)  ! You'll need a Base64 function
  
  ! Set up the request
  httpWebAddress = 'https://api.example.com/data'
  httpVerbMethod = HttpVerb:GET
  postData = ''                              ! Empty for GET
  requestParameters = ''                     ! Or add like 'id=123&type=json'
  contentType = 'application/json'           ! Adjust as needed
  
  ! Build the Authorization header (pipe-separated format)
  headers = 'Authorization|Basic ' & CLIP(EncodedCreds)
  
  ! Make the request
  ReturnValue = HttpReq.HttpWebRequest(httpWebAddress, httpVerbMethod, |
                                        postData, requestParameters, |
                                        contentType, headers, responseOut)
  
  ! Check the result
  IF ReturnValue = 0
    ! Success
    MESSAGE('Response: ' & CLIP(responseOut))
  ELSIF ReturnValue > 0
    ! Buffer too small
    MESSAGE('Buffer too small. Need ' & ReturnValue & ' bytes')
  ELSE
    ! Error occurred
    MESSAGE('Error: ' & CLIP(responseOut))
  END

Even AI calls CLIP where it is necessary and where it is not necessary.

Maybe it knows Clarion and is using defensive programming? :laughing:

Justin Wilson wore a belt AND suspenders because he wasn’t taking any chances.

Thank you very much, I’m doing the first tests, the authentication worked, it stopped showing the invalid user message.

Can I ask which version of Clarion you are using? I am pretty sure I had trouble using Basic Authentication in Clarion 10, but that may have been with POST requests.

I’m using Clarion 12.