eclipsecoapcalifornium

Californium Framework CoAP and PUT request


I am trying to do a request to coap server (er-rest-example) using Californium. I succesfully do a POST request. But with PUT I am getting a BAD REQUEST, I try using this URLs in url:

coap://[aaaa::c30c:0000:0000:0002]:5683/actuators/leds
coap://[aaaa::c30c:0000:0000:0002]:5683/actuators/leds? 
coap://[aaaa::c30c:0000:0000:0002]:5683/actuators/leds?color=r

But with no one get success. What I am doing wrong?.

This is my simple script:

package coap_client;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Timer;
import java.util.TimerTask;

import org.eclipse.californium.core.CoapClient;
import org.eclipse.californium.core.CoapResponse;
import org.eclipse.californium.core.coap.MediaTypeRegistry;

public class cliente {
    public static void main(String[] args) throws Exception {
        Timer timer;
        timer = new Timer();
        TimerTask task = new TimerTask(){
                @Override
                public void run(){
                    String url="coap://[aaaa::c30c:0000:0000:0002]:5683/actuators/leds";
                    URI uri= null;
                    try {
                        uri = new URI(url);
                    } catch (URISyntaxException e) {
                        e.printStackTrace();
                    }
                    CoapClient client = new CoapClient(uri);
                    CoapResponse response = client.put("color=r",MediaTypeRegistry.TEXT_PLAIN);             
                    System.out.println(response.isSuccess());                   
                    if (response!=null) {
                        byte[] myreponse=response.getPayload();
                        String respuesta2 = new String(myreponse);
                        System.out.println(respuesta2);
                        }
                }
        };
        timer.schedule(task, 10,10*1000);
    }

}

Solution

  • In Contiki er-rest-example, see the POST/PUT handler(1) for the LED CoAP resource. It expects a mode param without which you will get a BAD_REQUEST as response. I assume that has to go in the request body.