I have a arm based development board which runs a coap server, and it provide a firmware update coap path. I can use the coap-client (which use libcoap) on a ubuntu linux to upgrade its firmware successfully.
The command is "coap-client -m POST coap://[a_ipv6_address]:5683/fwupgrade -v 7 -b 512 -B 10 -f /home/firmware.bin".
As you can see there is no -t option, which means no media type specified.
However, when I use the this go-coap, there should be a media type specified for hte POST request, I tried both message.AppOctets and message.AppOcfCbor which didn't work.
Here is my sample code:
package main
import (
"context"
"fmt"
"log"
"os"
"time"
"github.com/plgd-dev/go-coap/v3/message"
"github.com/plgd-dev/go-coap/v3/udp"
)
func main() {
// Define the CoAP server address and resource path
serverAddress := "[target_ipv6]:5683"
resourcePath := "/fwupgrade"
filePath := "/firmware.bin"
file, err := os.Open(filePath)
if err != nil {
log.Fatalf("Error opening file: %v", err)
}
defer file.Close()
// Configure blockwise options to enable and set block size to 512 bytes
//blockwiseOpt := options.WithBlockwise(true, blockwise.SZX512, 30*time.Second)
//client, err := udp.Dial(serverAddress, blockwiseOpt, options.WithMaxMessageSize(1024*1024))
client, err := udp.Dial(serverAddress)
if err != nil {
log.Fatalf("Error dialing: %v", err)
}
defer client.Close()
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
resp, err := client.Post(ctx, resourcePath, message.AppOctets, file)
if err != nil {
log.Fatalf("Error sending request: %v", err)
}
fmt.Printf("Response Status Code: %v\n", resp.Code())
payload, err := resp.ReadBody()
if err != nil {
log.Fatalf("Error reading response body: %v", err)
}
fmt.Printf("Response Payload: %s\n", payload)
}
So my question is how can I mimic the coap-client command using this go-coap package correctly?
Thanks in advance.
I have a arm based development board which runs a coap server, and it provide a firmware update coap path. I can use the coap-client (which use libcoap) on a ubuntu linux to upgrade its firmware successfully.
The command is "coap-client -m POST coap://[a_ipv6_address]:5683/fwupgrade -v 7 -b 512 -B 10 -f /home/firmware.bin".
As you can see there is no -t option, which means no media type specified.
However, when I use the this go-coap, there should be a media type specified for hte POST request, I tried both message.AppOctets and message.AppOcfCbor which didn't work.
Here is my sample code:
package main
import (
"context"
"fmt"
"log"
"os"
"time"
)
func main() {
// Define the CoAP server address and resource path
serverAddress := "[target_ipv6]:5683"
resourcePath := "/fwupgrade"
filePath := "/firmware.bin"
}
So my question is how can I mimic the coap-client command using this go-coap package correctly?
Thanks in advance.