Using an Apex callout to Google Maps geocoding API, I want to authorize narrowly within Google to protect the client ID from being reused by anyone else. I believe the referer header needs to match the authorization in Google for this to work. What's the referer header for the apex callout by default? salesforce.com? How could I check this? Can I use setHeader to change the referer to something more specific to us e.g. companyname . salesforce.com? Thanks!
[SalesForce] Is it possible to use the setHeader method on HttpRequest Class to set the referer
Related Solutions
Remove the transient
keyword. It causes the variable not to get serialized.
Using the transient Keyword
Use the transient keyword to declare instance variables that can't be saved, and shouldn't be transmitted as part of the view state for a Visualforce page. For example:
Transient Integer currentTotal;
You can also use the transient keyword in Apex classes that are serializable, namely in controllers, controller extensions, or classes that implement the Batchable or Schedulable interface. In addition, you can use transient in classes that define the types of fields declared in the serializable classes.
Problem is...you can't serialize the request and response classes. You might find something like the below helpful:
Serializable Request
public class GenericRequest
{
String requestType;
String requestMethod;
String requestURI;
String requestParams;
String requestHeaders;
String requestBody;
public GenericRequest(RestRequest request)
{
this.requestType = 'Inbound';
this.requestMethod = request.httpMethod;
this.requestURI = request.requestURI;
this.requestParams = (request.params != null) ? JSON.serialize(request.params) : null;
this.requestHeaders = (request.headers != null) ? JSON.serialize(request.headers) : null;
this.requestBody = (request.requestBody != null) ? request.requestBody.toString() : null;
}
public GenericRequest(HttpRequest request)
{
this.requestMethod = request.getMethod();
this.requestURI = request.getEndpoint();
this.requestBody = request.getBody();
this.requestType = 'Outbound';
}
}
Serializable Response
public class GenericResponse
{
String responseBody;
Integer responseStatus;
public GenericResponse(RestResponse response)
{
if (response == null) return;
responseStatus = response.statusCode;
if (response.responseBody != null)
responseBody = response.responseBody.toString();
}
public GenericResponse(HttpResponse response)
{
if (response == null) return;
responseStatus = response.getStatusCode();
responseBody = response.getBody();
}
}
Queueable Constructor
final GenericRequest request;
public MyQueueable(HttpRequest input)
{
request = new GenericRequest(input);
}
@isTest classes are not supposed to be in the code coverage list, and should not count towards the code coverage rules (75% covered). Sometimes, however, things get messed up in the database, and you need to manually delete the erroneous coverage records.
To do this:
- Go to the Developer Console
- Go to the Query tab
- Enter the Query
SELECT Id, NumLinesCovered FROM ApexCodeCoverageAggregate
. - Click on the "Use Tooling API" checkbox
- Select all rows
- Click Delete to delete the rows
- Click on Test | Clear Test Coverage.
- Run your tests again.
Again, your MockGenerator class should not have coverage, and should not count against the coverage requirements. If they are, you need to force the system to exclude them by deleting the relevant records.
Best Answer
This is an old question, but it is actually possible to use the setHeader() method to set the Referer. I had to make sure it was spelled 'Referer' (notice the capitals and the old-school misspelling) but it did work for me when attempting to grab a resource off an AWS server.
I'm posting this for anyone who, like me, was wondering if this is possible and found this question.