How to send a API POST request in Java or Android?

Hello,
i’m struggling to send a POST request from my Android application written in Java to the REST API of openHAB. With the API explorer i found the POST request to change the state of my switch element by calling the following command:

curl -X POST “http://172.25.50.100:8080/rest/items/DummySwitch” -H “accept: /” -H “Content-Type: text/plain” -H “Authorization: Bearer xyz…” -d “ON”

But i don’t know how to send the plain command given by -d “ON” inside Java. Because with all the given libraries you need to send key-value pairs as data.

The GET requests are working fine but the POST request isn’t working.

Does anyone know how to send -d “ON” in Java? Or has anyone an example?

Thank you very much

Start here -

Note “it depends” on Android version in use.

Thank you.

I already found this topic but my problem is that there is always a pair of key and value is sent as data in a POST request. But with the given curl command from the API explorer there is only -d “ON” sent. And i can’t find a solution to only send these types of requests.

No it isn’t. That is what this part is about -

Try

Thank you very much.

I found the solution for the Android Volley library. You have to add the following method:

@Override
            public byte[] getBody(){
                return "OFF".getBytes();
            }

With this method you are able to send plain text commands like “ON” or “OFF”

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.