You need to sign in to do that
Don't have an account?
Marc D Behr
Sending email address in REST GET call
Hi,
I am writing a custom REST API and I can not get past the following and wanted to see if anybody had a solution.
I need to be able to lookup custom records that belong to a customer. The customer can be identified either by an externalID field or thier email address. I have a GET method written that works as follows:
GET /services/apexrest/Thermostat/byUser/{user}
where {user} can be either the externalID (an integer), a SF RecordID or an email address
So, if I do a GET of /services/apexrest/Thermostat/byUser/157 (157 being the externalID) I get back the thermostats that belong to user 157 exactly as expected with a 200 HTTP Status code. Sending a SF AccountID also works fine. But, if I instead send a GET /services/apexrest/Thermostat/byUser/user@here.com I get back a HTTP Status of 404 with the message
"The requested resource does not exist"
I tried URL encoding the string so I was sending GET /services/apexrest/Thermostat/byUser/user%40here.com to see if that was causing the issue, but I still get the same results. In the code, I added several system.debug messages so I can see what s happening and it looks like in both cases, the code runs 100% sucessfully to the end and exits cleanly, but yet I get an error only when sending the email address.
As far as I can tell, there seems to be an issue with the SF REST parsing when the URL contains the '@' sign.
Has anybody else seen this or maybe have a suggested solution?
Here is the code:
I am writing a custom REST API and I can not get past the following and wanted to see if anybody had a solution.
I need to be able to lookup custom records that belong to a customer. The customer can be identified either by an externalID field or thier email address. I have a GET method written that works as follows:
GET /services/apexrest/Thermostat/byUser/{user}
where {user} can be either the externalID (an integer), a SF RecordID or an email address
So, if I do a GET of /services/apexrest/Thermostat/byUser/157 (157 being the externalID) I get back the thermostats that belong to user 157 exactly as expected with a 200 HTTP Status code. Sending a SF AccountID also works fine. But, if I instead send a GET /services/apexrest/Thermostat/byUser/user@here.com I get back a HTTP Status of 404 with the message
"The requested resource does not exist"
I tried URL encoding the string so I was sending GET /services/apexrest/Thermostat/byUser/user%40here.com to see if that was causing the issue, but I still get the same results. In the code, I added several system.debug messages so I can see what s happening and it looks like in both cases, the code runs 100% sucessfully to the end and exits cleanly, but yet I get an error only when sending the email address.
As far as I can tell, there seems to be an issue with the SF REST parsing when the URL contains the '@' sign.
Has anybody else seen this or maybe have a suggested solution?
Here is the code:
@HttpGet global static List<Device__c> getDevice() { RestRequest req = RestContext.request; RestResponse res = RestContext.response; res.statusCode = 400; //failure // get the parameters from the command line String[] command = req.requestURI.substringAfter('Thermostat/').split('/'); String action = (command.size() >= 1) ? command[0] : ''; String userID = (command.size() >= 2) ? command[1] : ''; List<Device__c> devices = new List<Device__c>(); if(userID == '' ) { return devices; } if(action == 'byUser') { res.statusCode = 200; //set return status to good if(userID.contains('@')) { devices = [Select Name, System_ID__c, Zip_Postal_Code__c From Device__c where Customer__r.PersonEmail = :userID]; } else if (userID.length() >= 15){ // we received a SF ID devices = [Select Name, System_ID__c, Zip_Postal_Code__c From Device__c where Customer__c = :ID.valueOf(userID)]; } else { devices = [Select Name, System_ID__c, Zip_Postal_Code__c From Device__c where Customer__r.WebCustomerID__c = :Decimal.valueof(userID)]; } } return devices; }
The parser will only accept '.xml' or '.json' as 'extensions', so by having an email address at the end of the line (which will always end in some TLD), you get the error.
The workaround? Simply end the URI with a '/'. So my example of '/services/apexrest/Thermostat/byUser/user%40here.com' becomes '/services/apexrest/Thermostat/byUser/user%40here.com/' and everything is peachy fine.
All Answers
Can you try doing URL Encode while sending the email id as Rest parameter. Most probably it is not understanding @ parameter. You can encode the email string with UTF-8 format and it should work fine.
Please Mark this as Answer if it helps you
--
Regards,
Grazitti Team
Web: www.grazitti.com
As I said, I did try to encode the @ sign as %40 (/services/apexrest/Thermostat/byUser/user%40here.com) and I still get the same results :-(
Thanks anyway
Marc
--
Regards,
Grazitti Team
Web: www.grazitti.com
Then I sent /services/apexrest/Thermostat/byUser/user%40here.com
and the debug logs showed:
So you can see that it decoded the address and found 3 thermostats.
What I got as a result was HTTP Status "404 Not Found" and
[{"errorCode":"NOT_FOUND","message":"The requested resource does not exist"}]
Baffling.
The parser will only accept '.xml' or '.json' as 'extensions', so by having an email address at the end of the line (which will always end in some TLD), you get the error.
The workaround? Simply end the URI with a '/'. So my example of '/services/apexrest/Thermostat/byUser/user%40here.com' becomes '/services/apexrest/Thermostat/byUser/user%40here.com/' and everything is peachy fine.