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

Discussion in 'iOS Questions' started by CrownDeveloper, Feb 1, 2018.

  1. CrownDeveloper

    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:
    Code:
    {
        
    "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.
    Code:
    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: Feb 4, 2018
  2. BillMeyer

    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.
     
  3. CrownDeveloper

    CrownDeveloper Member Licensed User

    ATS is set to False.

    I tried the Post request from postman it is working fine
    upload_2018-2-1_18-4-17.png

    these are the headers
    upload_2018-2-1_18-5-44.png
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    application/x-www-form-urlencoded means that you need to use PostString with the parameters like:
    Code:
    $"username=${strUsername}&password=${strPassword}&grant_type=password"$
     
    BillMeyer likes this.
  5. CrownDeveloper

    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
    Code:
    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")
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    There are several mistakes here:
    1. This is not a application/x-www-form-urlencoded request.
    2. You should set the content type to application/json with SetContentType.
    3. The string should be a json string. Use JsonGenerator to create it.
     
  7. CrownDeveloper

    CrownDeveloper Member Licensed User

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

    Code:
    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

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

    Erel Administrator Staff Member Licensed User

    Why list?
     
  9. CrownDeveloper

    CrownDeveloper Member Licensed User

    As JsonGenerator accepts list, parden me if I am using the wrong Json generator
     
  10. CrownDeveloper

    CrownDeveloper Member Licensed User

    My bad, I found the another method and now my code looks like this
    Code:
    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
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    Tip, you can shorten your code:
    Code:
    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.
     
  12. CrownDeveloper

    CrownDeveloper Member Licensed User

    Thanks Erel, it worked.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice