I'm first time trying to get data from REST api using GET. I'm able to get data using postman where I just pass my url with GET property and passing two key value pairs in Headers tab like below and am able to get data records wise data in csv format (normal csv kind of output, column name with records)
Headers tab in Postman
autd_id:ddhd45t-78d0-54ad-8320-94aejit526
aith:token:hf2h1-d1a1-6589-c55d-94aejit526
Now am trying to do it myself using java, but am getting error code 401. It means I guess wrong credentials, but its right. Here is my code.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
public class Brigade {
public static void Getbrigadedata() {
try {
URL url = new URL(
"https://Brigade.co-buying.com/api/reports/referred/brigade_raf1");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
String userCredentials = "ddhd45t-78d0-54ad-8320-94aejit526:9658hf2h1-d1a1-6589-c55d-94aejit526";
new Base64();
String basicAuth = "Basic " + new String(Base64.encode(userCredentials.getBytes()));
conn.setRequestProperty("Authorization", basicAuth);
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Can anyone please tell me what am doing wrong ?