Search code examples
.netibm-api-management

consuming APIM api from .net application


My cross team member has created an API in APIM which I have to use for an existing .net application. He shared the postman collection and environment. I have imported it and successfully got the results back using Postman.

I have to call this API from an existing .net application and I don't know how to do it. Any pointers/inputs/help will be really appreciated.


Solution

  • Following is the solution I have implemented. I hope it helps someone. -- I used HttpClient to make the call and get the token.

    Dim client = New HttpClient()
    

    client.BaseAddress = "https://dev-azure.com" 'want the response to be JSON. client.DefaultRequestHeaders.Accept.Clear() client.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue("application/json"))

            'Build up the data to POST.
            Dim postData As List(Of KeyValuePair(Of String, String)) = New List(Of KeyValuePair(Of String, String))()
            postData.Add(New KeyValuePair(Of String, String)("grant_type", "yourValue"))
            postData.Add(New KeyValuePair(Of String, String)("client_id", "yourvalue"))
            postData.Add(New KeyValuePair(Of String, String)("client_secret", "yourvlaue"))
            postData.Add(New KeyValuePair(Of String, String)("resource", yourvlaue))
            Dim content = New FormUrlEncodedContent(postData)
    
            'Post to the Server And parse the response.
            Dim responseContent = client.PostAsync("token", content).Result
    
            If (responseContent.StatusCode = HttpStatusCode.OK) Then
                Dim jsonString = responseContent.Content.ReadAsStringAsync().Result
                Dim responseData = JsonConvert.DeserializeObject(jsonString)
                accessToken = responseData("access_token")
            End If
        End Using