Postman Samples / Usages

This includes Pre-request script for easy payload creation

Create a Java Springboot application with the HmacUtil and AesGCM Util shared:

HmacUtil - Sha512 - For Bpay OnlyAES-GCM-Util - For Bpay Only

Example SecurityController.java

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import org.springframework.web.bind.annotation.*;

import java.security.GeneralSecurityException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class SecurityController {

    @PostMapping("/generate-secure-request")
    public Map<String, String> generateSecureRequest(@RequestBody SecureRequest request) throws GeneralSecurityException {
        // Validate input
        if (request.getClientId() == null || request.getSecretKey() == null || request.getAesKey() == null || request.getRequestPayload() == null) {
            throw new IllegalArgumentException("Missing required fields in request.");
        }

        // Encrypt request payload
        String encRequestMsg = AESGCMUtil.encrypt(request.getRequestPayload(), request.getAesKey(), request.iv);

        // Generate timestamp
        String dateTime = String.valueOf(System.currentTimeMillis());

        Map<String, String> hmacTemp = new HashMap<>();
        hmacTemp.put("clientId", request.getClientId());
        hmacTemp.put("encRequestMsg", encRequestMsg);

        ObjectMapper objectMapper = new ObjectMapper();
        String hmacMessage = "";

        try {
            String jsonString = objectMapper.writeValueAsString(hmacTemp);
            hmacMessage = request.getClientId() + dateTime + jsonString;
            hmacMessage = hmacMessage.replace(":", ": ").replace(",", ", ");
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }


        String hmac = HMACUtil.calculateHMAC(request.getSecretKey(), hmacMessage);

        System.out.println("HMAC Input final (Java): " + hmacMessage);


        // Prepare response
        Map<String, String> response = new HashMap<>();
        response.put("clientId", request.getClientId());
        response.put("dateTime", dateTime);
        response.put("hmac", hmac);
        response.put("encRequestMsg", encRequestMsg);

        return response;
    }

    @Data
    public static class SecureRequest {
        private String clientId;
        private String secretKey;
        private String aesKey;
        private String iv;
        private String requestPayload;
    }


}

and ofcourse:

then the gradle dependencies:

Run the app, should run on 8080 port.

then the Postman Pre-Request script

in the body section of Postman , add:

Cheers! 🥳

This helps you debug / test faster!

Shared with ❤️ by Satyajit

Last updated

Was this helpful?