Android Question How to call .net 6.0 RestAPI in B4A

junaidahmed

Well-Known Member
Licensed User
Longtime User
I have used RestAPI in .net 6.0 (.net core).I would like to know how to call API (Post request) in B4A for below queries

1.Login (how to pass json string as body in B4A)
2.Read Data( with jwt authentication and json paramater )
3.Insert ( with jwt authentication and json paramater)
4.Update ( with jwt authentication and json paramater)
5.Delete ( with jwt authentication and json paramater)
 

mcqueccu

Well-Known Member
Licensed User
Longtime User
This should help

 
Upvote 0

junaidahmed

Well-Known Member
Licensed User
Longtime User
Thanks for reply...
I have refer above link but I haven't seen jwt authentication

Can you have an example of login, after that pass this token in post request
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
?
Not tested:

You need to provide more information about the API methods (GET, POST, DELETE, PUT, etc.)

The calls are all authorized with token (JWT) or the token must be requested from time to time.

Only 2 GET and POST examples are posted.

The token can be optional in some cases, for example when you request a token for a login.

B4X:
Public Sub TestURL
    Dim Link As String = "My URL....."
    
    Dim GetParameters() As String = Array As String ("Key1", "Value1", "Key2", "Value2")
    Dim Token As String = "" 'token JWT

    Wait For (GetURL(Link, GetParameters, Token)) Complete (DataResult As String)
    Log(DataResult)
    
    
    Dim Data As Map = CreateMap("User" : "xxxx", "Password" : "xxxxx")
    Dim PostParameters  As String = Data.As(JSON).ToString
    Dim Token As String = "" 'token JWT

    Wait For (PostURL(Link, PostParameters, Token)) Complete (DataResult As String)
    Log(DataResult)
End Sub


Public Sub PostURL(URL As String, Parameters As String, Token As String) As ResumableSub
    Dim Result As String
    Dim j As HttpJob
    Try
        j.Initialize("", Me)
        j.PostString (URL, Parameters)
        j.GetRequest.SetHeader("Content-Type","application/json")
        If Not(Token.Length = 0) Then j.GetRequest.SetHeader("Authorization", $"Bearer ${Token}"$)
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            Result = j.GetString
        End If
    Catch
        Log(LastException)
    End Try
    j.Release
    Return Result
End Sub

Public Sub GetURL(URL As String, Parameters() As String, Token As String) As ResumableSub
    Dim Result As String
    Dim j As HttpJob
    Try
        j.Initialize("", Me)
        j.Download2(URL, Parameters)
        j.GetRequest.SetHeader("Content-Type","application/json")
        If Not(Token.Length = 0) Then j.GetRequest.SetHeader("Authorization", $"Bearer ${Token}"$)
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            Result = j.GetString
        End If
    Catch
        Log(LastException)
    End Try
    j.Release
    Return Result
End Sub
 
Upvote 0

junaidahmed

Well-Known Member
Licensed User
Longtime User
?
Not tested:

You need to provide more information about the API methods (GET, POST, DELETE, PUT, etc.)

The calls are all authorized with token (JWT) or the token must be requested from time to time.

Only 2 GET and POST examples are posted.

The token can be optional in some cases, for example when you request a token for a login.

B4X:
Public Sub TestURL
    Dim Link As String = "My URL....."
   
    Dim GetParameters() As String = Array As String ("Key1", "Value1", "Key2", "Value2")
    Dim Token As String = "" 'token JWT

    Wait For (GetURL(Link, GetParameters, Token)) Complete (DataResult As String)
    Log(DataResult)
   
   
    Dim Data As Map = CreateMap("User" : "xxxx", "Password" : "xxxxx")
    Dim PostParameters  As String = Data.As(JSON).ToString
    Dim Token As String = "" 'token JWT

    Wait For (PostURL(Link, PostParameters, Token)) Complete (DataResult As String)
    Log(DataResult)
End Sub


Public Sub PostURL(URL As String, Parameters As String, Token As String) As ResumableSub
    Dim Result As String
    Dim j As HttpJob
    Try
        j.Initialize("", Me)
        j.PostString (URL, Parameters)
        j.GetRequest.SetHeader("Content-Type","application/json")
        If Not(Token.Length = 0) Then j.GetRequest.SetHeader("Authorization", $"Bearer ${Token}"$)
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            Result = j.GetString
        End If
    Catch
        Log(LastException)
    End Try
    j.Release
    Return Result
End Sub

Public Sub GetURL(URL As String, Parameters() As String, Token As String) As ResumableSub
    Dim Result As String
    Dim j As HttpJob
    Try
        j.Initialize("", Me)
        j.Download2(URL, Parameters)
        j.GetRequest.SetHeader("Content-Type","application/json")
        If Not(Token.Length = 0) Then j.GetRequest.SetHeader("Authorization", $"Bearer ${Token}"$)
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            Result = j.GetString
        End If
    Catch
        Log(LastException)
    End Try
    j.Release
    Return Result
End Sub
As per above code,I am trying to make post request but I m getting below error message.please help to resolve this issues

ResponseError. Reason: , Response: {"type":"https://tools.ietf.org/html/rfc7231#section-6.5.13","title":"Unsupported Media Type","status":415,"traceId":"00-5b8b1bbdd375f6cb4b91da088ab1e681-1b81882ae8a0efae-00"}
 
Upvote 0

junaidahmed

Well-Known Member
Licensed User
Longtime User
Copy the sample curl code from postman and paste here. It will help to know how the request is formed and sent
see the below screeshot
1687449294914.png
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
test:
B4X:
Public Sub TestURL
    Dim Link As String = "My URL....."

    Dim Body As Map = CreateMap("username" : "xxxx", "password" : "xxxxx")
    Dim Parameters  As String = Body.As(JSON).ToString

    Wait For (PostURL(Link, Parameters)) Complete (DataResult As String)
    Log(DataResult)
End Sub


Public Sub PostURL(URL As String, Parameters As String) As ResumableSub
    Dim Result As String
    Dim j As HttpJob
    Try
        j.Initialize("", Me)
        j.PostString (URL, Parameters)
        j.GetRequest.SetHeader("Content-Type","application/json")
        j.GetRequest.SetHeader("Content-length", Parameters.Length)
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            Result = j.GetString
        End If
    Catch
        Log(LastException)
    End Try
    j.Release
    Return Result
End Sub
 
Last edited:
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
test
1687491170789.png

B4X:
Public Sub TestURL
    Dim Link As String = "xxxxxxxxxxxxxxxxx/api/user/authenticate"

    Dim Body As Map = CreateMap("username" : "GDev", "password" : "xxxxx")
    Log(Body.As(JSON).ToString)
    Dim Parameters  As String = Body.As(JSON).ToString

    Wait For (PostURL(Link, Parameters)) Complete (DataResult As String)
'    Log(DataResult)
    Log(DataResult.As(JSON).ToMap.As(JSON).ToString)
End Sub


Public Sub PostURL(URL As String, Parameters As String) As ResumableSub
    Dim Result As String
    Dim j As HttpJob
    Try
        j.Initialize("", Me)
        j.PostString (URL, Parameters)
        j.GetRequest.SetContentType("application/json")
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            Result = j.GetString
        End If
    Catch
        Log(LastException)
    End Try
    j.Release
    Return Result
End Sub
1687491117242.png
 
Upvote 0

junaidahmed

Well-Known Member
Licensed User
Longtime User
test
View attachment 143168
B4X:
Public Sub TestURL
    Dim Link As String = "xxxxxxxxxxxxxxxxx/api/user/authenticate"

    Dim Body As Map = CreateMap("username" : "GDev", "password" : "xxxxx")
    Log(Body.As(JSON).ToString)
    Dim Parameters  As String = Body.As(JSON).ToString

    Wait For (PostURL(Link, Parameters)) Complete (DataResult As String)
'    Log(DataResult)
    Log(DataResult.As(JSON).ToMap.As(JSON).ToString)
End Sub


Public Sub PostURL(URL As String, Parameters As String) As ResumableSub
    Dim Result As String
    Dim j As HttpJob
    Try
        j.Initialize("", Me)
        j.PostString (URL, Parameters)
        j.GetRequest.SetContentType("application/json")
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            Result = j.GetString
        End If
    Catch
        Log(LastException)
    End Try
    j.Release
    Return Result
End Sub
View attachment 143167
Thanks working fine now....
 
Upvote 0

junaidahmed

Well-Known Member
Licensed User
Longtime User
Thanks working fine now....
sorry,Now I am trying to make Get request as per above example but its shows an errormessage as "(RuntimeException) java.lang.RuntimeException: Request does not support this method.".Please check my below request url and advise how to do this URL123.png
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
1687651401734.png

B4X:
Public Sub TestGETURL
    Dim Link As String = "https://localhost:7016/api/Master/GetMasterDetailInfo"
    Dim Parameters() As String = Array As String ("pFactoryID", "Mg==", "pEmployeeNo", "NDYzNA==", "pLoggedInUserID", "Mg==")
    Wait For (GetURL(Link, Parameters)) Complete (DataResult As String)
    Log(DataResult)
End Sub

Public Sub GetURL(URL As String, Parameters() As String) As ResumableSub
    Dim Result As String
    Dim j As HttpJob
    Try
        j.Initialize("", Me)
        j.Download2(URL, Parameters)
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            Result = j.GetString
        End If
    Catch
        Log(LastException)
    End Try
    j.Release
    Return Result
End Sub
 
Upvote 0

junaidahmed

Well-Known Member
Licensed User
Longtime User
sorry,Now I am trying to make Get request as per above example but its shows an errormessage as "(RuntimeException) java.lang.RuntimeException: Request does not support this method.".Please check my below request url and advise how to do this URLView attachment 143188
sorry,Now I am trying to make Get request as per above example but its shows an errormessage as "(RuntimeException) java.lang.RuntimeException: Request does not support this method.".Please check my below request url and advise how to do this URLView attachment 143188

View attachment 143192
B4X:
Public Sub TestGETURL
    Dim Link As String = "https://localhost:7016/api/Master/GetMasterDetailInfo"
    Dim Parameters() As String = Array As String ("pFactoryID", "Mg==", "pEmployeeNo", "NDYzNA==", "pLoggedInUserID", "Mg==")
    Wait For (GetURL(Link, Parameters)) Complete (DataResult As String)
    Log(DataResult)
End Sub

Public Sub GetURL(URL As String, Parameters() As String) As ResumableSub
    Dim Result As String
    Dim j As HttpJob
    Try
        j.Initialize("", Me)
        j.Download2(URL, Parameters)
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            Result = j.GetString
        End If
    Catch
        Log(LastException)
    End Try
    j.Release
    Return Result
End Sub
thanks working fine but how to check if there is any error response from api for example if there is no token etc (api error handling)
 
Last edited:
Upvote 0
Top