« Tip of the day: Make … | Home | DynaPDF 4, Optimizati… »

Using CURL with Rosette web service

One developer using MBS Plugin came with a request to connect to Rosette's API to query various functions on language detection and parsing.

From the website they got this cURL command line to translate:

curl "https://api.rosette.com/rest/v1/morphology/complete" \
     -H 'user_key: [your_api-key]' \
     -H 'Content-Type:application/json' \
     -H 'Accept:application/json' \
     -d'{"content": "The quick brown fox jumped over the lazy dog. Yes he did."}'

Now this translates very well into a FileMaker Script. As you see we start a new CURL session, set a couple of options, perform the transfer and check the result.

For building the JSON I highly recommend to check the latest 6.0 prerelease for the new JSON commands we have there. As this webservice is a REST webservice, the input and output is JSON encoded and our functions help to do this very efficiently. 

# Start new CURL transfer

Set Variable [$curl; Value:MBS("CURL.New")]

# Set options like Verbose Debug Messages

Set Variable [$result; Value:MBS("CURL.SetOptionVerbose"; $curl; 1)]

# the url of webservice

Set Variable [$result; Value:MBS("CURL.SetOptionURL"; $curl; "https://api.rosette.com/rest/v1/morphology/parts-of-speech")]

# the header info for tagging, API Key comes from a global field.

Set Variable [$result; Value:MBS("CURL.SetOptionHTTPHeader"; $curl; "user_key: " & RosetteAPISettings::RosetteAPIKey; "Content-Type: application/json"; "Accept: application/json" )]

# We want a POST transfer:

Set Variable [$result; Value:MBS("CURL.SetOptionPost"; $curl; 1 )]

# Build JSON using the plugin function to properly encode a string:

Set Variable [$request; Value:"{\"content\": " & MBS("JSON.CreateString"; TextData::TextAsInput) & "}"]

Set Variable [$result; Value:MBS("CURL.SetOptionPostFields"; $curl; $request)]

#show our JSON in a field for debugging:

Set Field [TextData::CURLInput; $request]

#Run transfer

Set Variable [$result; Value:MBS("CURL.Perform"; $curl)]

# Check result and debug messages

Set Field [TextData::CurlDebug; MBS("CURL.GetDebugAsText"; $curl)]

Set Field [TextData::CurlOutput; MBS("CURL.GetResultAsText"; $curl)]

# Cleanup

Set Variable [$r; Value:MBS("CURL.Cleanup"; $curl)]

17 01 16 - 14:11
No comments

Remember personal info?

Emoticons / Textile

Hide email:

Small print: All html tags except <b> and <i> will be removed from your comment. You can make links by just typing the url or mail-address.