Communicate with REST Web Services
•
|
New HTTP Request() supports redirects, for example, from HTTP to HTTPS.
|
The following examples communicate with the National Aeronautics and Space Administration (NASA) API. You must request a key from https://api.nasa.gov/index.html#apply-for-an-api-key and assign it to the key variable to run the scripts successfully.
key = "<key>"; // substitute your key
limit = 100;
url = "https://api.nasa.gov/patents/content";
request = New HTTP Request(
URL( url ), // the restAPI endpoint
Method( "HTTP method" ),
Query String( // define key pairs
[["api_key" => key, "concept_tags" => "false",
"limit" => limit]] // JSL associative array
)
);
data = request << Send(); // send the request to the API
Write( data );
{"count": 100, "results": [{"category": "materials and coatings", "client_record_id": "patent_ARC-14661-3", "center": "ARC", "eRelations": [], "reference_number": "ARC-14661-3", ...}]}
// JSON output written to the log
•
|
The query string is at the end of the URL, following “?”. For example, in https://www.google.com/search?q=jmp, “q=jmp” is the query string. New HTTP Request() automatically URI-encodes key/value pairs in a query string (and FORM).
|
key= "<key>"; // substitute your key
limit = 100;
url = "https://api.nasa.gov/patents/content";
request = New HTTP Request(
URL( url ),
Method( "Get" ),
Query String(
[["api_key" => key, "concept_tags" => "false",
"limit" => limit]]
)
);
json = request << Send();
jsl_json = Parse JSON( json );
// turns JSON data into a JSL associative array
results = jsl_json["results"];
Write( results );
{["_id" => "53f65b3d5904da2c9fc3008f", "abstract" => "Method and system for functionalizing a collection of carbon nanotubes (CNTs). A selected precursor gas (e.g., H.sub.2 or NH.sub.3 or ...}
// associative array
key= "<key>"; // substitute your key
limit = 100;
url = "https://api.nasa.gov/patents/content";
request = New HTTP Request(
URL( url ),
Method( "Get" ),
Query String(
[["api_key" => key, "concept_tags" => "false",
"limit" => limit]]
)
);
json = request << Send();
jsl_json = Parse JSON( json );
dt = JSON To Data Table( As JSON Expr( jsl_json["results"] ) );
// JSON to Data Table turns JSON data into a data table
// As JSON Expr turns a JSL associative array into a JSON string
dt << Set Name( "NASA Patents" );
Figure 14.5 Data Imported in a Data Table