@@ -107,6 +107,9 @@ private static class HostStatus {
107
107
/** This library's version. */
108
108
private final static String version = "3.12.0" ;
109
109
110
+ /** Maximum size for an API key to be sent in the HTTP headers. Bigger keys will go inside the body. */
111
+ private final static int MAX_API_KEY_LENGTH = 500 ;
112
+
110
113
// ----------------------------------------------------------------------
111
114
// Fields
112
115
// ----------------------------------------------------------------------
@@ -545,7 +548,18 @@ private byte[] _requestRaw(Method m, String url, String json, List<String> hosts
545
548
546
549
// set auth headers
547
550
hostConnection .setRequestProperty ("X-Algolia-Application-Id" , this .applicationID );
548
- hostConnection .setRequestProperty ("X-Algolia-API-Key" , this .apiKey );
551
+ // If API key is too big, send it in the request's body (if applicable).
552
+ if (this .apiKey != null && this .apiKey .length () > MAX_API_KEY_LENGTH && json != null ) {
553
+ try {
554
+ final JSONObject body = new JSONObject (json );
555
+ body .put ("apiKey" , this .apiKey );
556
+ json = body .toString ();
557
+ } catch (JSONException e ) {
558
+ throw new AlgoliaException ("Failed to patch JSON body" );
559
+ }
560
+ } else {
561
+ hostConnection .setRequestProperty ("X-Algolia-API-Key" , this .apiKey );
562
+ }
549
563
for (Map .Entry <String , String > entry : this .headers .entrySet ()) {
550
564
hostConnection .setRequestProperty (entry .getKey (), entry .getValue ());
551
565
}
0 commit comments