How to pass variable in HTTPRequest Header?

57 views Asked by At

I want to add header in my HTTPRequest with variable but it will give error not taking it but when i pass direct string value in inverted commas it work well.

Hers is the code in 3rd header i am passing variable checksum which contains String value but it is not working and if i pass direct String value it's work without problem.

HttpRequest request1 = (HttpRequest) HttpRequest.newBuilder()
                    .uri(URI.create("https://api-preprod.phonepe.com/apis/pg-sandbox/pg/v1/pay"))
                    .header("accept", "application/json")
                    .header("Content-Type", "application/json")
                    .header("X-VERIFY",checksum)
                    .method("POST", HttpRequest.BodyPublishers.ofString("{\"request\":\"\"}"))
                    .build();
1

There are 1 answers

2
Sergey Tsypanov On BEST ANSWER

The most probable explanation is that you haven't overridden toString() method in the class of your checksum. Try to override the method returning String value of checksum:

class Checksum {
 @Override
 public String toString() {
   return value;
 }
}

HttpRequestBuilder converts an object passed into header() method according to certain rules and for object of custom class it just calls toString() method.