Reputation: 1
I am trying to call Fidel's APIs from my java code, this API needs just a key for authentication that we need to pass in headers. when I hit the API from postman it works. but from my java code, it throws 401.
here is postman call: enter image description here
and this is my java code:
URL url = new URL(environment.getBaseUrl() + "offers");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept", "application/json");
con.setRequestProperty("Fidel-Key", "my-secret-key-here");
con.setDoOutput(true);
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
String json = ow.writeValueAsString(fidelCreateOfferRequest);
try (OutputStream os = con.getOutputStream()) {
byte[] input = json.getBytes(StandardCharsets.UTF_8);
os.write(input, 0, input.length);
}
int status = con.getResponseCode();
System.out.println("Http Status: " + status);
BufferedReader in;
if (status == 200) {
in = new BufferedReader(new InputStreamReader(con.getInputStream()));
} else {
in = new BufferedReader(new InputStreamReader(con.getErrorStream()));
System.out.println("cannot create offer. failed with status: " + status);
}
Upvotes: 0
Views: 28
Reputation: 1
This was happening because I was not sending one of the fields in request body, didn't notice that earlier.
Upvotes: 0