@@ -151,7 +151,7 @@ Params:
151
151
* urlPath - Signing URL, remember to append <<tenant >>.e.api.gov.sg or <<tenant >>-pvt.i.api.gov.sg in <<URL >>
152
152
* appId - App ID created in Gateway
153
153
* secret - set to null for REST L2 SHA256WITHRSA
154
- * formList - to support parameter for form data if any
154
+ * formData - to support parameter for form data if any
155
155
* password
156
156
* alias
157
157
* fileName
@@ -171,7 +171,7 @@ String alias = "alpha";
171
171
String appId = " <<appId>>" ;
172
172
String secret = null ;
173
173
// only needed for Content-Type: application/x-www-form-urlencoded, else null
174
- ApiList formList = null ;
174
+ ApiList formData = null ;
175
175
String nonce = null ;
176
176
String timestamp = null ;
177
177
@@ -181,19 +181,24 @@ String timestamp = null;
181
181
ApiList queryParam = new ApiList ();
182
182
queryParam. add(" query1" ," value1" );
183
183
184
- // optional for formList
185
- ApiList formList = new ApiList ();
186
- formList . add(" param1" , " data1" );
184
+ // optional for formData
185
+ formData = new ApiList ();
186
+ formData . add(" param1" , " data1" );
187
187
188
- // If queryParam and formList are both available, combine the list before submitting
189
- formList . addAll(queryParam);
188
+ // If queryParam and formData are both available, combine the list before submitting
189
+ formData . addAll(queryParam);
190
190
191
191
try {
192
- String signature = ApiSigning . getSignatureToken(authPrefix, authPrefix, httpMethod, signingUrl, appId, secret, formList , password, alias, certFileName, nonce, timestamp);
192
+ String signature = ApiSigning . getSignatureToken(authPrefix, authPrefix, httpMethod, signingUrl, appId, secret, formData , password, alias, certFileName, nonce, timestamp);
193
193
} catch (ApiUtilException e) {
194
194
e. printStackTrace();
195
195
}
196
196
```
197
+ ** NOTE**
198
+
199
+ For ** formData** parameter used for Signature generation, the key value parameters ** do not** need to be URL encoded,
200
+ When your client program is making the actual HTTP POST call, the key value parameters ** has** to be URL encoded (refer to ** formPostData** )
201
+
197
202
198
203
#### Constructing Signature BaseString (for reference only)
199
204
@@ -206,15 +211,15 @@ Params:
206
211
* appId - App ID created in Gateway
207
212
* urlPath
208
213
* httpMethod
209
- * formList - only needed for Content-Type: application/x-www-form-urlencoded
214
+ * formData - only needed for Content-Type: application/x-www-form-urlencoded
210
215
* nonce - set to null for random generated number
211
216
* timestamp - set to null for current timestamp
212
217
213
218
``` java
214
219
String signingUrl = " https://<<URL>>/api/v1/?param1=first¶m2=123" ;
215
220
216
- ApiList formList = new ApiList ();
217
- formList . add(" param1" , " data1" );
221
+ ApiList formData = new ApiList ();
222
+ formData . add(" param1" , " data1" );
218
223
219
224
String baseString;
220
225
@@ -225,7 +230,7 @@ baseString = ApiSigning.getBaseString(
225
230
" <<appId>>" ,
226
231
signingUrl,
227
232
" post" ,
228
- formList ,
233
+ formData ,
229
234
" 6584351262900708156" ,
230
235
" 1502184161702"
231
236
);
@@ -293,6 +298,89 @@ try {
293
298
e. printStackTrace();
294
299
}
295
300
301
+ ```
302
+ #### Sample HTTP POST Call for x-www-form-urlencoded with APEX L1 Security (for reference only)
303
+
304
+ ``` java
305
+
306
+ @Test
307
+ public void Http_Call_Test() throws ApiUtilException , IOException
308
+ {
309
+
310
+ String httpMethod = " POST" ;
311
+ // URL for actual HTTP API call
312
+ String url = " https://tenant.api.gov.sg:443/api14021live/resource" ;
313
+ // URL for passing as parameter for APEX Signature Token generation
314
+ String signUrl = " https://tenant.e.api.gov.sg:443/api14021live/resource" ;
315
+ String appId = " tenant-1X2w7NQPzjO2azDu904XI5AE" ;
316
+ String secret = " s0m3s3cr3t" ;
317
+ ApiList formData = new ApiList ();
318
+ formData. add(" key1" , " value1" );
319
+ formData. add(" key2" ," value2" );
320
+
321
+ String authorizationToken = ApiSigning . getSignatureToken(
322
+ realm
323
+ , authPrefixL1
324
+ , httpMethod
325
+ , signUrl
326
+ , appId
327
+ , secret
328
+ , formData
329
+ , null
330
+ , null
331
+ , null
332
+ , null
333
+ , null
334
+ );
335
+ System . out. println(" authorizationToken : " + authorizationToken);
336
+
337
+ try {
338
+ // ignore SSL
339
+ SSLContext sslContext = SSLContext . getInstance(" SSL" );
340
+ sslContext. init(null , getTrustManager(), new java.security. SecureRandom ());
341
+ HttpsURLConnection . setDefaultSSLSocketFactory(sslContext. getSocketFactory());
342
+
343
+ HttpURLConnection con = (HttpURLConnection ) new URL (url). openConnection();
344
+ con. setDoOutput(true );
345
+ con. setDoInput(true );
346
+ con. setRequestMethod(httpMethod);
347
+ con. setRequestProperty(" Content-Type" , " application/x-www-form-urlencoded" );
348
+ con. setRequestProperty(" charset" , " utf-8" );
349
+ con. setRequestProperty(" Authorization" , authorizationToken);
350
+ con. setUseCaches(false );
351
+ con. setConnectTimeout(5000 );
352
+ con. setReadTimeout(5000 );
353
+
354
+ DataOutputStream out = new DataOutputStream (con. getOutputStream());
355
+ ApiList formPostData = new ApiList ();
356
+ formPostData. add(" key1" ,URLEncoder . encode(" value1" , " UTF-8" ));
357
+ formPostData. add(" key2" ,URLEncoder . encode(" value2" , " UTF-8" ));
358
+ out. writeBytes(formPostData. toString(false ));
359
+ out. flush();
360
+ out. close();
361
+ System . out. println(" Start http call ..." );
362
+ int status = - 1 ;
363
+ status = con. getResponseCode();
364
+ System . out. println(" HTTP Status:" + status);
365
+
366
+ System . out. println(" End http call ..." );
367
+ BufferedReader in = new BufferedReader (new InputStreamReader (con. getInputStream()));
368
+ String inputLine;
369
+ StringBuffer content = new StringBuffer ();
370
+ while ((inputLine = in. readLine()) != null ) {
371
+ content. append(inputLine);
372
+ }
373
+
374
+ System . out. println(" Content:" + content);
375
+ in. close();
376
+ con. disconnect();
377
+ }catch (Exception e){
378
+ System . out. println(" Error executing Http_Call_Test() : " + e);
379
+ }
380
+ // force to true to pass the test case
381
+ assertTrue(true );
382
+ }
383
+
296
384
```
297
385
298
386
## Contributing
0 commit comments