I tried to write client and server for CoAP serivce in GoLang. I used https://github.com/dustin/go-coap for this.
I successfully able to call POST end point but not able to call PUT. Following are the questions I have
What I tried code: Client Code:
req := coap.Message{
Type: coap.Confirmable,
Code: coap.POST,
MessageID: 12345,
Payload: []byte(`Hello CoAP Server `),
}
path := "/App"
if len(os.Args) > 1 {
path = os.Args[1]
}
req.SetOption(coap.ETag, "weetag")
req.SetPathString(path)
c, err := coap.Dial("udp", "localhost:5683")
rv, err := c.Send(req)
Client Code:
req := coap.Message{
Type: coap.Confirmable,
Code: coap.PUT,
MessageID: 12345,
Payload: []byte(`Hello CoAP Server `),
}
path := "/App/fb" //here fb is app id and this can be anything
if len(os.Args) > 1 {
path = os.Args[1]
}
req.SetOption(coap.ETag, "weetag")
req.SetPathString(path)
c, err := coap.Dial("udp", "localhost:5683")
rv, err := c.Send(req)
Server Code for POST and PUT requests:
mux := coap.NewServeMux()
// TODO-later: use UDP/coap
mux.Handle("/App", coap.FuncHandler(Register))
mux.Handle("/App/{AppID}", coap.FuncHandler(UpdateApp))
err := coap.ListenAndServe("udp", ":5683", mux)
Here is server code for update application by PUT request. Here I dont know how I can specify endpoint as PUT in CoAP. Can someone suggest me how I can create this endpoint mux.Handle("/App/{AppID}", coap.FuncHandler(UpdateApp))
as PUT?
Or even let me know if there is better Go library for CoAP Go Services?
According to this example:
https://github.com/dustin/go-coap/blob/master/example/server/coap_server.go
looks like a go-coap handler is only bound by URI. I think you'll have to handle request of different types by yourself.
A request with an unrecognized or unsupported Method Code MUST generate a 4.05 (Method Not Allowed) piggybacked response.
From: CoAP RFC 7252 Section 5.8 Method definitions
Unfortunately I didn't learn go and never used go-coap so cannot show a snippet. Though I think it would be pretty easy: you just need to response back with NON-4.05 with the same MID and token.