REST Web Services

13
I'd like to consume some RESTful webservices (such as yahoo flickr) and import the data in my system. Does anyone have any examples of how I could accomplish something like this in Mendix?
asked
3 answers
9

Well, I don't know exactly what services you'd like to consume, but here's an example of a simple rest call:

public static void main(String[] args) {
        call("{'username' : 'simon' }", "http://www.example.com/user/");
    }

    private static void call(String req, String url) {
        URLConnection conn;
        String result = "";

        try {
            conn = new URL(url.toString()).openConnection();
            conn.setDoOutput(true);
            conn.setRequestProperty("Content-Type", "application/json");

            OutputStreamWriter outWriter = new OutputStreamWriter(conn.getOutputStream());
            outWriter.write(req.toString());
            outWriter.flush();

            String cookie = conn.getHeaderField("Set-Cookie");
            System.out.println("Cookie : " + cookie);

            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;

            while ((line = reader.readLine()) != null) {
                result += line;
            }
            outWriter.close();
            reader.close();

            System.out.println("Received " + result);
        } catch (Exception e) {
            // TODO : Error handling
            System.out.println("Server Exception : " + e.getMessage());

        }
    }

Hope that helps :) Note that you still need to process the result using a JSON library, but that's pretty specific. If you need more help on that I'd recommend either reading up on the web on parsing JSON in java or asking a new question (although parsing JSON is a little offtopic for this forum, you should probably check stackoverflow for more info)

answered
6

REST web services are not supported yet by the webservice call action within microflows.

You can of course use a Java action to execute a REST call, see the answer of Achiel for an example.

answered
1

http://www.mendix.com/blog/consuming-first-rest-service/

answered