PHP:
https://www.geeksforgeeks.org/how-to-receive-json-post-with-php/
// Takes raw data from the request
$json = file_get_contents('php://input');
// Converts it into a PHP object
$data = json_decode($json);
Java:
class WebhookController {
WebhookController() {
}
@PostMapping("/notify")
void notify(@RequestBody String body) {
// body is the received payload
}
}
C#:
using System.Web.Http;
using System.IO;
using System.Web;
namespace AnyNameSpace
{
/// <summary>
/// Controller
/// </summary>
public class MyApiController : ApiController
{
/// <summary>
/// Request method
/// </summary>
[HttpPost]
[Route("AnyRoute")]
[AllowAnonymous]
public HttpResponseMessage Data()
{
using (StreamReader reader = new StreamReader(HttpContext.Current.Request.InputStream))
{
string str = reader.ReadToEnd();
// Process the received data here
}
return new HttpResponseMessage(HttpStatusCode.OK);
}
}
}
Python:
# Python: Version 3.7 and above, dependencies: fastapi, uvicorn
# coding=utf8
import hashlib
import uvicorn
from fastapi import FastAPI, Request, Body
app = FastAPI()
# Verify if the signature matches
def verify_sign(sign, data):
# Your secret key
secret_key = '***************'
my_str = str(data, encoding='utf8') + '/' + secret_key
my_sign = hashlib.sha256(my_str.encode('utf8')).hexdigest()
return sign == my_sign
@app.get('/')
def hello():
"""Renders a sample page."""
return "Hello World!"
# API route
@app.post("/test/url")
def query(request: Request, data=Body(None)):
'''
:param data: Incoming parameter structure
'''
print(data)
headers = request.headers
if 'sign' in headers:
sign = headers['sign']
# Signature verification
if verify_sign(sign, data):
print('verify success')
res = {'code': 200, 'message': 'verify success'}
else:
print('verify fail')
res = {'code': 400, 'message': 'verify fail'}
else:
print('sign error')
res = {'code': 400, 'message': 'sign error'}
return res
if __name__ == '__main__':
# data_sign is the script name
uvicorn.run(app='data_sign:app', host="0.0.0.0", port=5000, reload=True, debug=True, access_log=True)