iOS Question Posting a login request to ASP.Net Web API

CrownDeveloper

Member
Licensed User
I am stuck on a login request to web API. So the process is simple I have to send “username”, “password” and “grant_type” to the server along with the two headers “Accept” and “Content-Type”. If the user is valid my app will receive the following response from the server:
B4X:
{
    "access_token": "XXXX",
    "token_type": "bearer",
    "expires_in": 86399
}
Now in the subsequent request I have to send the token.


I tried to send a request with Download2 and PostString but couldn’t get any response and the request was time out with code 400.

below is my code to send request.
B4X:
Private Sub btnEnter_Click
    Dim j As HttpJob
    Dim Map1 As Map
    
    If txtName.Text<>"" And txtPassword.Text<>"" Then
        Dim strName As String = txtName.text
        Dim strPassword As String = txtPassword.text

        j.Initialize("", Me)
        j.Download2("http://xxxxx.azurewebsites.net/oauth/token", _
       Array As String("username",strName,"password",strPassword,"grant_type","password"))

    j.GetRequest.SetContentType("application/x-www-form-urlencoded")
    j.GetRequest.SetHeader("Accept", "application/json")
    j.GetRequest.SetHeader("Content-Type", "application/json")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log(j.GetString)
    End If
        Log("Err! " & j.GetString)
    j.Release
Else
        Msgbox("Enter all fields and try again","Login")
End If
End Sub
P.S. I tried it using Postman the request is working fine
 
Last edited:

BillMeyer

Well-Known Member
Licensed User
At a quick glance:

What is your ATS# Setting in Main ?

Is your server capable of https ? Can you try with that ?

I have not looked the rest of your code - it seems to be OK.
 

CrownDeveloper

Member
Licensed User
Hi Erel,

I tried it in the following manner but still getting bad request whereas, the postman is working fine. should I send you the URL so that you can try
B4X:
 j.Initialize("", Me)
 j.PostString("http://xxxxx.azurewebsites.net/oauth/token",$"username=${strName}&password=${strPassword}&grant_type=password"$)
 j.GetRequest.SetContentType("application/x-www-form-urlencoded")
 j.GetRequest.SetHeader("Accept", "application/json")
 j.GetRequest.SetHeader("Content-Type", "application/json")
 

CrownDeveloper

Member
Licensed User
Thanks for the correction. Now, my code looks as below:

B4X:
Sub getJSON() As String
 Dim oKeyValueList As List
 oKeyValueList.Initialize
 oKeyValueList.Add(CreateMap("username":"abc","password":"abc123","grant_type":"password")) 
 Dim oJSONString As String
 Dim oJSONGenerator As JSONGenerator
 oJSONGenerator.Initialize2(oKeyValueList)
 oJSONString=oJSONGenerator.ToString
 Return oJSONString
End Sub

Private Sub btnEnter_Click
 Dim j As HttpJob
 Dim Map1 As Map
 If txtName.Text<>"" And txtPassword.Text<>"" Then
  Dim strName As String = txtName.text
  Dim strPassword As String = txtPassword.text
  j.Initialize("", Me)
    Dim jstr As String=getJSON
  Log(jstr)
  j.PostString("http://tshda.azurewebsites.net/oauth/token",jstr)
  j.GetRequest.SetContentType("application/json")
 'j.GetRequest.SetHeader("Accept", "application/json")
 Wait For (j) JobDone(j As HttpJob)
 If j.Success Then
 
  Log(j.GetString)
  pg.ResignFocus
  menu.Show
 End If
  Log(j.GetString)
 j.Release
 Else
  Msgbox("Enter all fields and try again","Login")
 End If
End Sub
the JSON method returns the following string

B4X:
[{"password":"abc123","username":"abc","grant_type":"password"}]
but still, I am getting status code 400
 

CrownDeveloper

Member
Licensed User
My bad, I found the another method and now my code looks like this
B4X:
Sub getJSON() As String
 Dim keyValue As Map
 keyValue = CreateMap("username":"abc","password":"abc123","grant_type":"password")
 Dim oJSONString2 As String
 Dim oJSONGenerator2 As JSONGenerator
 oJSONGenerator2.Initialize(keyValue)
 oJSONString2=oJSONGenerator2.ToString
 Return oJSONString2
End Sub
Still I am getting status code 400
 

Erel

Administrator
Staff member
Licensed User
Tip, you can shorten your code:
B4X:
Sub getJSON() As String
 Dim keyValue As Map = CreateMap("username":"abc","password":"abc123","grant_type":"password")
 Dim oJSONGenerator2 As JSONGenerator
 oJSONGenerator2.Initialize(keyValue)
 Return oJSONGenerator2.ToString
End Sub
I think that I previously misunderstood the postman screenshot.

The request is a application/x-www-form-urlencoded request. Not a json request.
I'm not sure why you set the content-type header to json in post #5. This is a mistake.
Though I don't think that it is related to the error you received.

The code in post #5 looks correct except of the headers. Remove them.
It is possible that you need to set the user agent header.
 
Top