Last active
January 2, 2022 00:44
-
-
Save arjunkomath/56cfde8117f7c65e0696488c38785dca to your computer and use it in GitHub Desktop.
Push by Techulus
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.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"))); | |
} | |
} |
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
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)); | |
} | |
}); |
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 | |
// 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() . " | |
"; |
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
# 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') | |
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 '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 |
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
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" | |
}' |
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
// 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