2018-04-06 09:06:36
{
"channel_id": "123456",
"product_id": "ubibot-ws1p",
"serial": "123456XXXXX",
"feeds": [{
"created_at": "2022-04-24T09:06:56Z",
"field1": 24.495308
}, {
"created_at": "2022-04-24T09:06:56Z",
"field2": 82
}, {
"created_at": "2022-04-24T09:07:00Z",
"field5": -34
}],
"status": "mac=98:7b:f3:10:a1:e2,usb=1",
"ssid_base64": "VFBHUw=="
}
<?php
try{
$data = file_get_contents('php://input'); //get POST payload, 获取post包文
$json = json_decode($data, true); //parse input data into json, 解析json,并进行业务逻辑
//-----------注意: 请确保业务逻辑在15秒之内完成,否则服务器端将做超时处理------------//
$myfile = file_put_contents('log.txt', $data.PHP_EOL , FILE_APPEND | LOCK_EX);
echo "SUCCESS"; //成功处理请求
}
catch (Exception $ex)
{
echo "ERROR";//失败处理请求返回
}
const http = require('http');
const server = http.createServer(function (req, res) {
let type = req.headers["content-type"];
if (req.method.toLowerCase() === 'post' && type === "application/json") {
try{
////get POST payload, 获取post包文
var jsonData = "";
req.on("data", function(chunk) {
jsonData += chunk;
});
req.on("end", function() {
//parse input data into json, 解析json,并进行业务逻辑
var reqObj = JSON.parse(jsonData);
//-----------注意: 请确保业务逻辑在15秒之内完成,否则服务器端将做超时处理------------//
var fs = require('fs');
fs.writeFile("log.txt", JSON.stringify(reqObj), function(err) {
if(err) {
res.end('ERROR');//失败处理请求返回
}
});
res.end('SUCCESS');//成功处理请求
});
}catch{
res.end('ERROR');//失败处理请求返回
}
}else{
res.end('ERROR');//失败处理请求返回
}
});
//设置监听端口
server.listen(8080, "127.0.0.1", function () {
console.log("server is started listen port 8080");
});
from http.server import BaseHTTPRequestHandler, HTTPServer
import cgi
import json
class testHTTPServer_RequestHandler(BaseHTTPRequestHandler):
def do_POST(self):
try:
ctype, pdict = cgi.parse_header(self.headers.get('content-type'))
if ctype == 'application/json':
#get POST payload, 获取post包文, parse input data into json, 解析json,并进行业务逻辑
length = int(self.headers['content-length'])
jsonArray = json.loads(self.rfile.read(length))
#-----------注意: 请确保业务逻辑在15秒之内完成,否则服务器端将做超时处理------------#
myfile = open("log.txt",'a')
myfile.write(json.dumps(jsonArray))
myfile.write("\n")
myfile.close()
#成功处理请求
self.protocal_version = 'HTTP/1.1'
self.send_response(200)
self.send_header("Welcome", "Contect")
self.end_headers()
self.wfile.write(bytes("SUCCESS", "utf-8"))
return
else:
#失败处理请求返回
self.protocal_version = 'HTTP/1.1'
self.send_response(300)
self.send_header("Welcome", "Contect")
self.end_headers()
self.wfile.write(bytes("ERROR", "utf-8"))
return
except:
#失败处理请求返回
self.protocal_version = 'HTTP/1.1'
self.send_response(300)
self.send_header("Welcome", "Contect")
self.end_headers()
self.wfile.write(bytes("ERROR", "utf-8"))
return
def run():
port = 8080
print('starting server, port', port)
# Server settings
server_address = ('', port)
httpd = HTTPServer(server_address, testHTTPServer_RequestHandler)
print('running server...')
httpd.serve_forever()
if __name__ == '__main__':
run()
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace demo1.Controllers
{
public class FeedData
{
public DateTime created_at { get; set; }
public object field1 { get; set; }
public object field2 { get; set; }
public object field3 { get; set; }
public object field4 { get; set; }
public object field5 { get; set; }
public object field6 { get; set; }
public object field7 { get; set; }
public object field8 { get; set; }
public object field9 { get; set; }
public object field10 { get; set; }
}
public class DeviceData
{
public string channel_id { get; set; }
public string status { get; set; }
public List feeds { get; set; }
public string ssid_base64 { get; set; }
}
[Route("api/[controller]")]
[ApiController]
public class RestApiController : ControllerBase
{
[HttpPost]
[Route("recv")]
public string ReceiveData([FromBody] DeviceData data)
{
try
{
//使用data进行业务处理
return "SUCCESS";//成功处理请求
}
catch (Exception)
{
return "ERROR";//失败处理请求返回
}
}
}
}