CoAP
CoAP copied to clipboard
POST and DELETE methods
What would it take to add the POST and DELETE methods as well. I have tried, but don't think I understand the packets.
Could you please show me your code and server environment here? I will check that.
The server is a basic express api server running locally. In a HTTP environment it would look like this.
// respond with "Hello World!" on the homepage
app.get('/', function (req, res) {
res.send('Hello World!');
});
// accept POST request on the homepage
app.post('/', function (req, res) {
res.send('Got a POST request');
});
// accept PUT request at /user
app.put('/user', function (req, res) {
res.send('Got a PUT request at /user');
});
// accept DELETE request at /user
app.delete('/user', function (req, res) {
res.send('Got a DELETE request at /user');
});
Since is it COAP the current server is looking more like this:
var coap = require('coap'),
server = coap.createServer();
server.on('request', function(req, res) {
if(req.method.value == "GET") {
console.log("Got a GET request")
}
if(req.method == "POST") {
console.log("Got a GET request")
}
if(req.method == "PUT") {
console.log("Got a GET request")
}
if(req.method == "DELETE") {
console.log("Got a GET request")
}
});
// the default CoAP port is 5683
server.listen();
Tried to modify your code by adding to coap.cpp
uint16_t Coap::get(IPAddress ip, int port, char *url) {
return this->send(ip, port, url, COAP_TYPE::COAP_CON, COAP_METHOD::COAP_GET, NULL, 0, NULL, 0);
}
uint16_t Coap::post(IPAddress ip, int port, char *url) {
return this->send(ip, port, url, COAP_TYPE::COAP_CON, COAP_METHOD::COAP_GET, NULL, 0, NULL, 0);
}
uint16_t Coap::put(IPAddress ip, int port, char *url, char *payload, int payloadlen) {
return this->send(ip, port, url, COAP_TYPE::COAP_CON, COAP_METHOD::COAP_PUT, NULL, 0, (uint8_t *)payload, payloadlen);
}
uint16_t Coap::delete(IPAddress ip, int port, char *url, char *payload, int payloadlen) {
return this->send(ip, port, url, COAP_TYPE::COAP_CON, COAP_METHOD::COAP_PUT, NULL, 0, (uint8_t *)payload, payloadlen);
}
and to coap.h
uint16_t get(IPAddress ip, int port, char *url);
uint16_t post(IPAddress ip, int port, char *url, char *payload, int payloadlen);
uint16_t put(IPAddress ip, int port, char *url, char *payload, int payloadlen);
uint16_t delete(IPAddress ip, int port, char *url);
But I underestimated your work. It didn't work for me.
Okay, this COAP library is reference and some buggy code, not fully including RFC 7252 about a optional header/proxy...etc. And I mean show your Photon code, your code is only server side. I check this library on microcoap and libcoap, so If you use other library(nodecoap?) I don't know what you use. I will debug this library with same your environment.