HTTP Request Header

Nickle

Member
Licensed User
Longtime User
I am working on a project and need to pass HTTP request header for token Generation and use the token to pass other parameters for other endpoints. Each endpoint call is a combination of header and parameter call as indicated below. I am a new to this and running out of time and struggling figure it out. Any one who will like to work with me on this and also support in future snippets for a cost.

Below required.....

1. Call API to Generate Token

Request URL


localhost/payment_apis/index.php/pay/token

Request Headers

B4X:
headers:
Content-Type: application/json

parameters:
{
"pos_id":"1001"
}

2. Call API to receive payment:

localhost/payment_apis/index.php/pay/receive

B4X:
headers:
   Content-Type: application/json
   api_key:  token from token api

parameters:
{
  "CustomerName": "Andrew Mark",
  "CustomerMsisdn": "0556786372",
  "CustomerEmail": "[email protected]",
  "Channel": "mtn-gh",
  "Amount": 0.8,
  "Description": "T Shirt",
  "ClientReference": "",
  "Token": "123445"
 }
 

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Try something like this for the first call

B4X:
sub call
    Private JOB As HttpJob
    Private jsonstring As String
    Private jp As JSONGenerator
  
    Private jsonmap As Map
  
    jsonmap.Initialize
  
    jsonmap.Put("pos_id","1001")
    ' You can other things to the map here

    jp.Initialize(jsonmap)
    jsonstring = jp.ToString
  
   ' this is assuming your api was a POST.
    JOB.Initialize("", Me)
    JOB.PostString("localhost/payments_apis/index.php/pay/token",jsonstring)
    JOB.Getrequest.SetContentType("application/json")
      
    wait for (JOB)  jobdone(jd As HttpJob)
    If jd.Success Then
       ' add some appropriate error handling
       Private jgen As JSONParser
       jgen.Initialize(jd.GetString)
       ' assuming a JSON map is the return object
       Private returnmap As Map = jgen.NextObject
     
     
   Else
       ' handle error condition
   End If

    jd.Release  
end sub

The second call is similar just need other items in the map.
to add the api key to the header you need

B4X:
   JOB.GetRequest.SetHeader("api_key",token from token api) ' this is how you can add the api key to the header

Edited: to add the missing JOB.Initialize call.
 
Last edited:
Top