Question

I have a web application in which I use jQuery to get data from a controller to refresh a page. The request passes along a json object containing nested arrays. The request in jQuery goes like this:

    $.get("resources", { categories: myJson })
        .done(function (resultdata) {
etc...

This call works fine, and gets me the data I need.

Now, the problem is, I want to be able to call the same method in this controller from a desktop Java application. Here's what I have:

public class HttpBasicAuth {

    public void connect() {
        try {

            URL url = new URL("http://someurl.com/resources/resources");
            String creds = "some.name@somecompany.com:somepassword";
            byte[] encoded = Base64.encodeBase64(creds.getBytes("UTF-8"));
            String encoding = new String(encoded);

            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setDoOutput(true);
            connection.setRequestProperty("Authorization", "Basic " + encoding);
            connection.setRequestProperty("Content-Type", "application/json");

            String input = "{'categories': [['8'],['33']]}";

            OutputStream os = connection.getOutputStream();
            os.write(input.getBytes());
            os.flush();

            if (connection.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
                throw new RuntimeException("Failed : HTTP error code : "
                        + connection.getResponseCode());
            }

            InputStream content = (InputStream) connection.getInputStream();
            BufferedReader in
                    = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

I have just tried supplying the Json as a string here, but that is just for testing. This whole code is taken from an example I found googling on passing json in java request. When I get it working I'll use a library for creating the json.

But I'm not sure I'm getting the json right. It doesn't work anyway.

So, the jQuery call does work. The Java one doesn't. So how can I call this controller method in Java exactly equivalent of the jQuery call that I know works?

EDIT:

Ok, apparently the json was not correctly created, in my example. I have now changed that part to the suggested String input = "{\"categories\": [[\"8\"],[\"33\"]]}";

However, this didn't solve the problem. I'm still not getting the correct results. I'm also unsure if the passing of json needs to be a POST request, I have trouble getting it to work at all with GET.

However, here are the two controller methods I've used for testing:

public function getResources()
    {

        $arr = array
        (array('8'),array('33'));
        $json = json_encode($arr);
        $outer_AND = json_decode($json, true);

        $query = $this->getQuery($outer_AND);
        $results = DB::connection('ccms')->select($query);

        return $results;
    }

    public function postResources()
    {
        $input = Input::get('categories');
        $outer_AND = array();
        if ($input != null) {
            $outer_AND = json_decode($input, true);
        }

        $query = $this->getQuery($outer_AND);
        $results = DB::connection('ccms')->select($query);

        return $results;
    }

The POST method is now the one I'm actually trying to use, the other one just simulates the values as php arrays to check what the results should be. So I'm trying to get the values from the Input, but it doesn't work.

EDIT 2:

Ok, I now found when checking that the parameter is empty, so it is apparently not getting sent at all. I have done the change to POST and I have changed the json according to the suggestions here. But still the post parameter is empty when it gets to the controller method so it doesn't matter.

What is wrong? Why is the parameter not sent?

Was it helpful?

Solution

String input = "{'categories': [['8'],['33']]}";

Isn't valid JSON change it to:

String input = "{\"categories\": [[\"8\"],[\"33\"]]}";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top