arsenal88
arsenal88

Reputation: 1160

Authentication Details Not Provided REST API POST Request

I'm posting to an API server and I'm getting the following reply:

Server Response: {"detail":"Authentication credentials were not provided."}

I'm writing the requests in Java and I'm not sure exactly why it's saying credentials were not provided.

Here is a snippet of my code:

 hconn.setRequestProperty("Content-Type", "application/json");
 hconn.setRequestProperty( "Accept", "application/json" );
 if( urlconn instanceof HttpsURLConnection )
 {
    String encoded = new String(Base64.getEncoder().encode( ( username + ":" + password).getBytes()));
    String auth = "Basic " + encoded;
    urlconn.setRequestProperty("Encoded Authorization", auth );

 }

Where hconn is of type HttpURLConnection

This is the only relvant snippet that you guys need. Are there properties i'm missing to set here?

I know the server response is from a Django framework but the documentation is not clear on spotting what IS required to prevent this.

Any help appreciated.

Upvotes: 0

Views: 167

Answers (1)

Prasann
Prasann

Reputation: 1291

Authorization is the correct request header name and not 'Encoded Authorization'. So, you should set

urlConn.setRequestProperty("Authorization", auth);

Upvotes: 1

Related Questions