Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POST and DELETE methods #2

Open
bayees opened this issue Jul 25, 2015 · 3 comments
Open

POST and DELETE methods #2

bayees opened this issue Jul 25, 2015 · 3 comments

Comments

@bayees
Copy link

bayees commented Jul 25, 2015

What would it take to add the POST and DELETE methods as well. I have tried, but don't think I understand the packets.

@hirotakaster
Copy link
Owner

Could you please show me your code and server environment here? I will check that.

@bayees
Copy link
Author

bayees commented Jul 27, 2015

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.

@hirotakaster
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants