0

I have written Angularjs service as shown below.It retrieves data from the 3rd party service.It's working fine.

Now I have a requirement to write a WebApi method for the same.The reason for that is, we can consume that service from various types of applications.i.e. desktop, web and mobile.How can I implement such a service?

AngulaJS service:

(function () {
    appModule.service('getPropertyDetailsByUsingApiService', ['$http', function ($http) {
        this.propertyDetails = function (token, number, street, county, zip) {
            var endpointUrl = 'http://myaddress.com/api/AddressMatcher?Token=';
            var url = endpointUrl + token + '&Number=' + number + '&Street=' + street + '&County=' + county + '&Zip=' + zip;

            return $http.get(url).then(function (data) {
                var result = data;
                if (result.data[0].Status == 'OK') {
                    return $http.get(endpointUrl + token + '&Apn=' + result.data[0].Result[0].APN + '&County=' + county)
                        .then(function (finalData) {
                            return finalData;
                        });
                } else {
                    return null;
                }
            });
        };
    }
    ]);
})();

WebApi method :

    [HttpGet]
    public async Task<MyModelDto> GetPropertyDetailsByUsingApiService()
    {
        //I would like to have a help here to implement it

        return result;
    }
3
  • What are you expecting in output? Can you elaborate? Commented Feb 14, 2017 at 4:15
  • I can manage the output.I just need to know how to call the above mentioned 3rd party services within the web API. @Pavvy Commented Feb 14, 2017 at 4:47
  • Please check my answer. Commented Feb 14, 2017 at 9:14

2 Answers 2

1

I guess you are looking for HttpClient.GetAsync.

For example,

var response = await client.GetAsync("http://...");
if (response.IsSuccessStatusCode) {
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

0

Use this,

using (HttpClient client = new HttpClient())
        {
            client.BaseAddress = new Uri(apiDetails.BaseUrl);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));                
            HttpResponseMessage response = client.PostAsJsonAsync(apiDetails.RequestUrl, obj).Result;                
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.