Skip to content

Instantly share code, notes, and snippets.

@arjunkomath
Last active Nov 11, 2019
Embed
What would you like to do?
Push by Techulus
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import java.io.IOException;
import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
public class TestClass {
@Test
public void testHttpCall() throws IOException {
// given
HttpPost request = new HttpPost("https://push.techulus.com/api/v1/notify/02f5b175-7085-449c-a86f-a3a4854eedb3");
request.add("Content-Type", "application/json; charset=utf-8");
// when
HttpResponse response = HttpClientBuilder.create().build().execute(request);
// then
HttpEntity entity = response.getEntity();
String jsonString = EntityUtils.toString(entity);
// and if the response is
// {
// "status": "OK"
// }
// Then we can assert it with
assertThat(jsonString, hasJsonPath("$.status", is("OK")));
}
}
request
.post('https://push.techulus.com/api/v1/notify/02f5b175-7085-449c-a86f-a3a4854eedb3')
.send({
"title": "Push by Techulus",
"body": "This is your first push notification"
})
.set('Content-Type', 'application/json; charset=utf-8')
.redirects(0)
.end(function(err, res){
if (err || !res.ok) {
console.log('Oh no! error');
} else {
console.log('yay got ' + JSON.stringify(res.body));
}
});
<?php
// Include Guzzle. If using Composer:
// require 'vendor/autoload.php';
use GuzzleHttp\Pool;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
$client = new Client();
$request = new Request(
"POST",
"https://push.techulus.com/api/v1/notify/02f5b175-7085-449c-a86f-a3a4854eedb3",
[
"Content-Type" => "application/json; charset=utf-8"
],
"{\"body\":\"This is your first push notification\",\"title\":\"Push by Techulus\"}");
$response = $client->send($request);
echo "Response HTTP : " . $response->getStatusCode() . "
";
# Install the Python Requests library:
# `pip install requests`
import requests
import json
def send_request():
try:
response = requests.post(
url="https://push.techulus.com/api/v1/notify/02f5b175-7085-449c-a86f-a3a4854eedb3",
headers={
"Content-Type": "application/json; charset=utf-8",
},
data=json.dumps({
"title": "Push by Techulus",
"body": "This is your first push notification"
})
)
print('Response HTTP Status Code: {status_code}'.format(
status_code=response.status_code))
print('Response HTTP Response Body: {content}'.format(
content=response.content))
except requests.exceptions.RequestException:
print('HTTP Request failed')
require 'net/http'
require 'net/https'
require 'json'
def send_request
uri = URI('https://push.techulus.com/api/v1/notify/02f5b175-7085-449c-a86f-a3a4854eedb3')
# Create client
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
dict = {
"title" => "Push by Techulus",
"body" => "This is your first push notification"
}
body = JSON.dump(dict)
# Create Request
req = Net::HTTP::Post.new(uri)
# Add headers
req.add_field "Content-Type", "application/json; charset=utf-8"
# Set body
req.body = body
# Fetch Request
res = http.request(req)
puts "Response HTTP Status Code: #{res.code}"
puts "Response HTTP Response Body: #{res.body}"
rescue StandardError => e
puts "HTTP Request failed (#{e.message})"
end
curl -X "POST" "https://push.techulus.com/api/v1/notify/your_api_key" \
-H 'Content-Type: application/json; charset=utf-8' \
-d $'{
"title": "Push by Techulus",
"body": "This is your first push notification"
}'
// Using Alamofire - https://cocoapods.org/pods/Alamofire
func sendTestPush() {
// Add Headers
let headers = [
"Content-Type":"application/json; charset=utf-8",
]
// JSON Body
let body: [String : Any] = [
"title": "Push by Techulus",
"body": "This is your first push notification"
]
// Fetch Request
Alamofire.request("https://push.techulus.com/api/v1/notify/02f5b175-7085-449c-a86f-a3a4854eedb3", method: .post, parameters: body, encoding: JSONEncoding.default, headers: headers)
.validate(statusCode: 200..<300)
.responseJSON { response in
if (response.result.error == nil) {
debugPrint("HTTP Response Body: \(response.data)")
}
else {
debugPrint("HTTP Request failed: \(response.result.error)")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment