I have a Rest service which I consume like this:
URL url = new URL(URI);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
String input = "{\"qty\":100,\"name\":\"iPad 4\"}";
OutputStream os = conn.getOutputStream();
os.write(input.getBytes());
os.flush();
That is one of the ones that I try and it works perfectly, but now I need to consume another Rest but unlike the previous one, this new one needs an authentication value to be configured in the header.
How do I set a variable called 'authentication' with a value of 'x' in the request header?
With the HTTP headers ( headers
URLConnection#setRequestProperty(String key, String value)
) of the request are configured , either for the default headers (which you were already using), or for custom headers, as in your case.Only one more line needs to be added after the connection is instantiated .