2

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 ?

Elena
  • 181
  • 1
  • 10
  • Please provide your UI code because 401 will most probably be caused by wrong credentials sent from UI (in header), since you are able to do so through Postman – Dhruv Singhal Jul 04 '18 at 05:04
  • @DhruvSinghal, But could you please check Is it a right way to pass auth_id and auth_token key value pair value in my code ? In postman we pass like I've mentioned above in my question – Elena Jul 04 '18 at 05:06
  • try https://stackoverflow.com/questions/496651/connecting-to-remote-url-which-requires-authentication-using-java – Dhruv Singhal Jul 04 '18 at 05:10
  • Are you using any kind of authentication? Or the authentication mechanism are just the two headers you mention? – akortex91 Jul 04 '18 at 05:12
  • I guess you will somehow need to include both key value pairs while authentication. – Dhruv Singhal Jul 04 '18 at 05:13
  • @Aris, Yes just these two key value pairs in headers are authentication. – Elena Jul 04 '18 at 06:04

1 Answers1

2

From what I understand, based on the information you provided the API you are trying to consume uses an authentication mechanism based on the existence of the two headers (along with their values) mentioned.

When you perform the call via Postman, you set these headers in the header tab of the tool. Problem is though that in your code, you never do that. Instead you attempt to create a string that contains the base64 encoded value of the credentials.

Assuming again, that the API requires the headers to be in place, I would suggest to try and set those to you request and attempt to perform the call. In that sense you code should look something like:

conn.setRequestProperty("<header_1>", "<header_value1>");
conn.setRequestProperty("<header_2>", "<header_value2>");

This should work for you fine -- again given that the authentication mechanism of the API is based on the headers you're mentioning.

akortex91
  • 3,290
  • 2
  • 13
  • 31