发布日期: 04/13/2021

Examples of HTTP Requests

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.

Write Data to the Log

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

Notes: 

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).

When you put an associative array in either a query string or a form, the key/value pairs get URI encoded. Spaces, ampersands, and other characters get encoded to ASCII, and the ampersand is escaped. The encoding is a “%” encoding.

The web service defines the valid key/value pairs that it accepts. Refer to the API documentation for valid key/value pairs.

Return Data as an Associative Array and Write it to the Log

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

Return Data as a JMP Data Table

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 

需要更多信息?有问题?从 JMP 用户社区得到解答 (community.jmp.com).