B4J Question a b4x code where api keys are used and get data from an API

Anurag Jain

Member
Licensed User
Hi Everyone.

I need help, in translating this info in a b4x code ---
API_URL=https://api.quantumnumbers.anu.edu.au
API_KEY=your_secret_api_key
curl -X GET -H "x-api-key:${API_KEY}" "${API_URL}?length=[array length]&type=[data type]&size=[block size]"

request inputs / help in making this code

Regards,
Anurag Jain
 
Solution
it runs, but gives error when passing api key - Job.GetRequest.SetHeader("x-api-key", API_KEY)

when I made the python program, it runs successfully
Try put the Job.GetRequest.SetHeader after Job.Download(url).

B4X:
Dim Job As HttpJob
Job.Initialize("QuantumNumbersJob", Me)
Job.Download(url)
Job.GetRequest.SetHeader("x-api-key", API_KEY)
Wait For (Job) JobDone(Job As HttpJob)

aeric

Expert
Licensed User
Longtime User
Using an AI coder (Qwen2.5 Coder Demo), here is what I got.

To translate the given curl command into B4X using OkHttpUtils2 with the Wait For statement, you can follow these steps. Below is an example of how you might write this in a B4X (B4A, B4J, or B4i) application:

  1. Add OkHttpUtils2 Library: Ensure that you have added the OkHttpUtils2 library to your project. You can do this through the B4X Libraries panel in your IDE.
  2. Write the Code: Use the CallSubDelayed2 and Wait For statements to handle the asynchronous HTTP request.
Here's a sample code snippet for B4X (B4J):

B4X:
' Main module
Sub Process_Globals
    Public Const API_URL As String = "https://api.quantumnumbers.anu.edu.au"
    Public Const API_KEY As String = "your_secret_api_key"
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    ' Initialize the form
    MainForm = Form1
    MainForm.Show
   
    ' Example parameters
    Dim arrayLength As Int = 10
    Dim dataType As String = "hex16"
    Dim blockSize As Int = 1
   
    ' Call the function to fetch data
    FetchQuantumNumbers(arrayLength, dataType, blockSize)
End Sub

Sub FetchQuantumNumbers(arrayLength As Int, dataType As String, blockSize As Int)
    Dim url As String = $"${API_URL}?length=${arrayLength}&type=${dataType}&size=${blockSize}"$
   
    Dim Job As HttpJob
    Job.Initialize("QuantumNumbersJob", Me)
    Job.GetRequest.SetHeader("x-api-key", API_KEY)
    Job.Download(url)
   
    ' Wait for the job to complete
    Wait For (Job) JobDone(Job As HttpJob)
   
    If Job.Success Then
        Dim response As String = Job.GetString
        Log("Response: " & response)
        ' Process the response here
    Else
        Log("Error: " & Job.ErrorMessage)
    End If
   
    Job.Release
End Sub

Explanation:​

  • Process_Globals: This section is used to declare global constants that are accessible throughout the entire application.
  • AppStart: This is the entry point of the application. It initializes the main form and calls the FetchQuantumNumbers subroutine.
  • FetchQuantumNumbers: This subroutine constructs the URL, initializes the HttpJob, sets the header, and downloads the data. It then waits for the JobDone event to process the response.

Additional Notes:​

  • MainForm: Ensure you have a form named MainForm in your project. You can rename it if necessary, but make sure to update the AppStart method accordingly.
  • API_KEY: Replace "your_secret_api_key" with your actual API key.
  • Parameters: Adjust the parameters (arrayLength, dataType, blockSize) as needed for your use case.
This code will make an HTTP GET request to the specified API endpoint, include the API key in the header, and log the response or error message to the console.
 
Last edited:
Upvote 0

Anurag Jain

Member
Licensed User
it runs, but gives error when passing api key - Job.GetRequest.SetHeader("x-api-key", API_KEY)

when I made the python program, it runs successfully
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
it runs, but gives error when passing api key - Job.GetRequest.SetHeader("x-api-key", API_KEY)

when I made the python program, it runs successfully
Try put the Job.GetRequest.SetHeader after Job.Download(url).

B4X:
Dim Job As HttpJob
Job.Initialize("QuantumNumbersJob", Me)
Job.Download(url)
Job.GetRequest.SetHeader("x-api-key", API_KEY)
Wait For (Job) JobDone(Job As HttpJob)
 
Upvote 1
Solution
Top