0

I have a java application with this code :

URL url = new URL("http://myurl/");
HttURLConnection connection = (HttURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutplut(true);
connection.setRequestProperty("Content-Type", "application/json");
BufferedWriter buffer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
buffer.write("{\"foo:\"0}");
buffer.flush();

I just want to do the samething in my navigatour URL bar.

Edit

I found a tool to modifier headers. Here a screenshoot of the dev tool when I load my page. Now where did I put my Json object?

header request

Julien Maret
  • 579
  • 1
  • 4
  • 22
  • 3
    `Content-Type` should be inside header not query. – Aniket Sahrawat Mar 28 '19 at 16:44
  • 1
    And `{\"foo\":bar}` in the body rather than the query parameters. Also that isn't valid JSON, which seems suspicious. – Aaron Mar 28 '19 at 16:47
  • yeah, it is {\"foo:\"bar}. taht not the important part – Julien Maret Mar 28 '19 at 16:52
  • @AniketSahrawat how did I put insided the hearder? – Julien Maret Mar 28 '19 at 16:53
  • @JulienMaret You are doing it right in the code snippet. I will suggest you to add `connection.setRequestProperty("Accept", "application/json");` along with it. It may or may not change the output by server. Also the JSON string that you are trying to pass is invalid. The correct JSON would be `"{\"foo\":\"bar\"}"` – Aniket Sahrawat Mar 28 '19 at 17:01

1 Answers1

1

If you need to send JSON data to your URL your code should be like this,

            URL url = new URL("http://myurl/");
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setDoOutput(true);
            con.setRequestMethod("POST");
            con.setRequestProperty("Content-Type", "application/json");

            String input = "{\"foo\":\"bar\"}";
            OutputStream ous = con.getOutputStream();
            ous.write(input.getBytes());
            ous.flush();

            if (con.getResponseCode() != HttpURLConnection.HTTP_OK)
            {
                throw new RuntimeException("Failed : HTTP error code : " + con.getResponseCode());
            }else
            {
                BufferedReader br = new BufferedReader(new InputStreamReader((con.getInputStream())));
                String output;
                System.out.println("Output from Server .... \n");
                while ((output = br.readLine()) != null) 
                {
                     System.out.println(output);
                }
            }

            con.disconnect();

If you need GET Method then you can place this,

con.setRequestMethod("GET");
con.setRequestProperty("Accept", "application/json");

If you need to send Request Body with the URL you can use CURL. And also you can use POSTMAN. By using this you can send requests and receive the response.

CURL will be like this,

curl -v -H "Content-Type: application/json" -X POST \
     -d '{\"foo\":\"bar\"}' http://myurl/

You can use Firefox to perform what you need, Read the 2nd answer.

Hasitha Jayawardana
  • 2,326
  • 4
  • 18
  • 36