Pipe and formatting command(?)

The following code was never going to work… but I am stumped on who I can format(?) the command to work in this instance with all the " and \ etc

Dan

(
var p, l;
p = Pipe.new("curl -X POST -H "Content-Type: application/json" \
    -d '{"userId": 1, "title": "Hello World", "body": "Post body."}' \
    https://example.com/posts", "r");            
	l = p.getLine;                    
	while({l.notNil}, {l.postln; l = p.getLine; });    
	p.close;                    
)

Even though I need help understanding the command in the Pipe, I changed your code in two variations as follows:

(
var p, l;
p = Pipe.new("curl -X POST -H" + "Content-Type: application/json".quote + "\ -d '{" + "userId".quote + ": 1, "+ "title".quote + ": "+ "Hello World".quote + ", " + "body".quote + ": " + "Post body.".quote + "}' \ https://example.com/posts", "r");            
l = p.getLine;                    
while({l.notNil}, {l.postln; l = p.getLine; });    
p.close;                    
)
(
var p, l;
p = Pipe.new("curl -X POST -H \"Content-Type: application/json\" \ -d '{\"userId\": 1, \"title\": \"Hello World\", \"body\": \"Post body.\"}' \ https://example.com/posts", "r");            
l = p.getLine;                    
while({l.notNil}, {l.postln; l = p.getLine; });    
p.close;                    
)

Their outputs are as follows:

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
		<title>404 - Not Found</title>
	</head>
	<body>
		<h1>404 - Not Found</h1>
		<script type="text/javascript" src="//wpc.75674.betacdn.net/0075674/www/ec_tpm_bcon.js"></script>
	</body>
</html>
-> 0

Is the result you intended?

According the following webpage,

I slightly changed the command in your text as follows:

(
var p, l;
p = Pipe.new("curl -X POST -H \"Content-Type: application/json\" \ -d '{\"userId\": 5, \"title\": \"Post Title\", \"body\": \"Post content.\"}' \ https://jsonplaceholder.typicode.com/posts", "r");            
l = p.getLine;                    
while({l.notNil}, {l.postln; l = p.getLine; });    
p.close;                    
)

The result is now as follows:

{
  "userId": 5,
  "title": "Post Title",
  "body": "Post content.",
  "id": 101
}
-> 0

It is interesting, but I am still curious how this could be used in musical context.

I think that I can get JSON file online using your code template, for example, from the following address:

http://www.khoa.go.kr/api/oceangrid/tideObsAirTemp/search.do?ServiceKey=wldhxng34hkddbsgm81lwldhxng34hkddbsgm81l==&ObsCode=DT_0001&Date=20160101&ResultType=json

The expected result is as follows:

{ 
"result" :{
"meta": {
"obs_post_id": "DT_0001",
"obs_post_name": "인천",
"obs_lat": "37.451983",
"obs_lon": "126.592111",
"obs_last_req_cnt":"800/20000"
},
"data": [
{
"record_time": "2016-01-01 00:01:00",
"air_temp": "14.61"
},
{
"record_time": "2016-01-01 00:03:00",
"air_temp": "14.62"
},
{
"record_time": "2016-01-01 00:04:00",
"air_temp": "14.65"
}
]
}

However, I get only the following line:

{"result":{"error":"ObsCode no information"}}
-> 0

when evaluating the following code:

(
var p, l;
p = Pipe.new("curl http://www.khoa.go.kr/api/oceangrid/tideObsAirTemp/search.do?ServiceKey=wldhxng34hkddbsgm81lwldhxng34hkddbsgm81l==&ObsCode=DT_0001&Date=20160101&ResultType=json", "r");
l = p.getLine;
while({l.notNil}, {l.postln; l = p.getLine; });
p.close;
)

Could you (or anyone) help me? I think I can easily map the data for sonification using some data utilising this code template!

you need to surround the URL with single quotes!

the culprit is the & character which needs to be escaped to use with bash…

(
var p, l;
p = Pipe.new("curl 'http://www.khoa.go.kr/api/oceangrid/tideObsAirTemp/search.do?ServiceKey=wldhxng34hkddbsgm81lwldhxng34hkddbsgm81l==&ObsCode=DT_0001&Date=20160101&ResultType=json'", "r");
l = p.getLine;
while({l.notNil}, {l.postln; l = p.getLine; });
p.close;
)
2 Likes

Yes that would be correct. example.com doesn’t have a REST api - I was just curious about formatting the request and chucked that in their. @semiquaver answer works.

Thanks! I could easily get all data! Only the important thing is how to make an attractive result with those data…

(
var p, l;
~json = "";
p = Pipe.new("curl 'http://www.khoa.go.kr/api/oceangrid/tideObsAirTemp/search.do?ServiceKey=wldhxng34hkddbsgm81lwldhxng34hkddbsgm81l==&ObsCode=DT_0001&Date=20160101&ResultType=json'", "r");
l = p.getLine;
while { l.notNil } {~json = ~json ++ l; l = p.getLine; };
p.close;
~dic = ~json.parseJSON
)

Post <<< ~dic

~dic.keys

~dic["result"].keys

~dic["result"]["meta"].keys

~dic["result"]["data"]

~time = ~dic["result"]["data"].collect { |item| item["record_time"].postln }
~temp = ~dic["result"]["data"].collect { |item| item["air_temp"].asFloat }
~temp.plot
2 Likes