Last active
May 25, 2016 07:55
-
-
Save hatimn02/175c75d561dc164f86893c098a3fd582 to your computer and use it in GitHub Desktop.
Via Socket Sample Codes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> | |
<script> | |
$(document).ready(function(){ | |
$("button").click(function(){ | |
$.post("http://flow.viasocket.com/trigger/sample_url/slack_integration"+"?Auth-Key="+"YOUR_AUTHKEY", | |
{ | |
name: "Donald Duck", | |
city: "Duckburg" | |
}); | |
}); | |
}); | |
</script> | |
</head> | |
<body> | |
<button>Invoke Socket Api</button> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Load the request module | |
var request = require('request'); | |
//Lets configure and request | |
request({ | |
url: 'http://flow.viasocket.com/trigger/sample_url/slack_integration', //Replace with your socket api | |
qs: {from: 'socket sample', time: +new Date()}, //Query string data | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json', | |
'Auth-Key': 'YourAuthKey' //Your Auth Key | |
}, | |
body: '{"Hello":"World"}' //Set the body as a string | |
}, function(error, response, body){ | |
if(error) { | |
console.log(error); | |
} else { | |
console.log(response.statusCode, body); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// The data to send to the API | |
$postData = array( | |
'contact_no' => '999999999', | |
'caller_phone' => '888888888', | |
'company' => 'socket', | |
'branch_info' => ‘India', | |
); | |
$header = array("Auth-Key: YOUR_AUTH_KEY"); | |
$URL = 'http://flow.viasocket.com/trigger/sample_url/slack_integration'; //Replace with your socket api | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL,$URL); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); | |
curl_setopt($ch, CURLOPT_POST, TRUE); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); | |
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code | |
$result=curl_exec ($ch); | |
curl_close ($ch); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from urllib import urlencode | |
from urllib2 import Request, urlopen | |
authkey = 'YOUR_AUTHKEY' # Set your Auth-Key here | |
url = 'http://flow.viasocket.com/trigger/sample_url/slack_integration' # Set your socket URL here | |
post_fields = {'foo': 'bar'} # Set POST fields here | |
request = Request(url, urlencode(post_fields).encode()) | |
request.add_header('Auth-Key', authkey) | |
json = urlopen(request).read().decode() | |
print(json) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "net/http" | |
require "uri" | |
require 'json' | |
base_uri = "http://flow.viasocket.com/trigger/team/flow_identifier" #Set your Socket Api | |
base_uri = base_uri + "?param1=value1¶m2=value2" #Set query params | |
uri = URI.parse(base_uri) | |
data = { | |
status: "testing", | |
comment: "Tested via socket." | |
} | |
http = Net::HTTP.new(uri.host, uri.port) | |
request = Net::HTTP::Post.new(uri.request_uri) | |
request.add_field("Auth-Key", "SETYOURAUTHKEY") #Set your Auth-Key in header | |
request.add_field("Content-Type", "application/json") | |
request.body = data.to_json #Set json body | |
#request.set_form_data({"param1" => "value1", "param2" => "value2"}) #Set form-data if not using raw body | |
response = http.request(request) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.json.simple.JSONObject; | |
import java.io.*; | |
import java.net.URL; | |
import java.net.URLConnection; | |
import java.net.URLEncoder; | |
public class Socket { | |
public static void invoke() { | |
//Your authentication key | |
String authKey = "YourAuthKey"; | |
//Prepare Url | |
URLConnection myURLConnection=null; | |
URL myURL=null; | |
BufferedReader reader=null; | |
//Socket Api | |
String mainUrl="http://flow.viasocket.com/trigger/sample_url/slack_integration"; //Replace with your socket api | |
mainUrl = mainUrl + "?"; | |
//You can post data in both query params as well as in body | |
//Prepare parameter or query string | |
StringBuilder sbPostData= new StringBuilder(mainUrl); | |
sbPostData.append("owner="+encode("John Parker")); //URL encoding to deal with special characters and spaces | |
sbPostData.append("&lat="+"65.326"); | |
sbPostData.append("&message="+encode("Hello World")); | |
//Prepare Json body | |
JSONObject jsonBody=new JSONObject(); | |
jsonBody.put("Humble", "Jumble"); | |
jsonBody.put("Greetings", "Hey, How r u?"); | |
//Final url | |
mainUrl = sbPostData.toString(); | |
try | |
{ | |
//Prepare connection | |
myURL = new URL(mainUrl); | |
myURLConnection = myURL.openConnection(); | |
myURLConnection.setDoOutput(true); | |
myURLConnection.setDoInput(true); | |
//Setting Auth-Key in header | |
myURLConnection.setRequestProperty ("Auth-Key", authKey); | |
//Setting Content-Type in header | |
myURLConnection.setRequestProperty ("Content-Type", "application/json; charset=UTF-8"); | |
OutputStreamWriter wr= new OutputStreamWriter(myURLConnection.getOutputStream()); | |
wr.write(jsonBody.toString()); | |
wr.flush(); | |
myURLConnection.connect(); | |
reader= new BufferedReader(new InputStreamReader(myURLConnection.getInputStream())); | |
//reading response | |
String response; | |
while ((response = reader.readLine()) != null) | |
//print response | |
System.out.println(response); | |
//finally close connection | |
reader.close(); | |
} | |
catch (IOException e) | |
{ | |
e.printStackTrace(); | |
} | |
} | |
private static String encode(String text){ | |
try{ | |
return URLEncoder.encode(text,"UTF-8"); | |
}catch(IOException e){ | |
e.printStackTrace(); | |
return null; | |
} | |
} | |
public static void main(String gg[]){ | |
Socket.invoke(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment