Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to call ibm watson api from .net core

I'm trying to call the watson personality insight api, after looking around it seems the solution is to make a .net equivalent of the following curl request. I'm pretty new to this and was wondering if i could get guidance or be pointed to relevant tutorials.

curl -X POST -u "{username}:{password}"
--header "Content-Type: application/json"
--data-binary @profile
"https://gateway.watsonplatform.net/personality-insights/api/v3/profile?version=2016-10-20&consumption_preferences=true&raw_scores=true"
like image 418
lagfvu Avatar asked Mar 25 '17 10:03

lagfvu


2 Answers

You can use the Watson Developer Cloud .NET Standard SDK. Install the Personality Insights service via NuGet using

Install-Package IBM.WatsonDeveloperCloud.PersonalityInsights -Pre

Instantiate the service

// create a Personality Insights Service instance
PersonalityInsightsService _personalityInsights = new PersonalityInsightsService();

// set the credentials
_personalityInsights.SetCredential("<username>", "<password>");

Call the service

var results = _personalityInsights.GetProfile(ProfileOptions.CreateOptions()
                                                         .WithTextPlain()
                                                         .AsEnglish()
                                                         .AcceptJson()
                                                         .AcceptEnglishLanguage()
                                                         .WithBody("some text"));

In a future release you will be able to call the service using named parameters instead of building options.

var results = _personalityInsights.GetProfile(
        "<input>", 
        "<content-type>", 
        "<content-language>", 
        "<accept>", 
        "<accept-language>",
        "<raw-scores>",
        "<csv-headers>"
        "<consumption-preferences>",
        "<version>"
    );
like image 199
taj Avatar answered Oct 23 '22 11:10

taj


in this case are you use curl to call the API? According your example...

Call the Personality Insights by providing the username and password that are provided in the service credentials for the service instance that you want to use. The API uses HTTP basic authentication.

For authentication:

curl -u "{username}":"{password}"
"https://gateway.watsonplatform.net/personality-insights/api/v3/{method}"

Bluemix collects data from all requests and uses the data to improve the Watson services.

Request logging:

curl -u "{username}":"{password}"
--header "X-Watson-Learning-Opt-Out: true"
"https://gateway.watsonplatform.net/personality-insights/api/v3/{method}"

Methods to call and get the response:

curl -X POST -u "{username}:{password}"
--header "Content-Type: application/json"
--data-binary @profile.json
"https://gateway.watsonplatform.net/personality-insights/api/v3/profile?version=2016-10-20&consumption_preferences=true&raw_scores=true"

IBM Watson API's uses standard HTTP response codes to indicate whether a method completed successfully.

  • 200-level response always indicates success.

  • 400-level response indicates some sort of failure.

  • 500-level response typically indicates an internal system error.

Check this documentation from IBM to develop, has all examples how to call and if have errors the reason for that. And this for verify how to work and how to use.

Demo here, you can fork from github if you want.

like image 35
Sayuri Mizuguchi Avatar answered Oct 23 '22 11:10

Sayuri Mizuguchi