You need to sign in to do that
Don't have an account?
fer_far
Call an URL/Web Service from Salesforce
Hi community,
I need to connect to an URL from Salesforce, I mean, from a VF page. The URL returns an XML and I need to process it.
The URL is http://www.ctt.pt/pdcp/xml_pdcp?incodpos=1000234
where incodpos parameter is a Portuguese postal code.
I don't know the best way to proceed. Can anyone help me out with this, please?
Thanks in advance.
Regards.
Hi Fer_far ,
You can do a HTTP Request and do a Callout to any url you want, and read whatever is returned as a String (xml in your case)
You can find the example i posted, and more by searching HttpRequest in : http://www.salesforce.com/us/developer/docs/apexcode/salesforce_apex_language_reference.pdf
Let me know if this is what you needed,
Gaston.
All Answers
Hi Fer_far ,
You can do a HTTP Request and do a Callout to any url you want, and read whatever is returned as a String (xml in your case)
You can find the example i posted, and more by searching HttpRequest in : http://www.salesforce.com/us/developer/docs/apexcode/salesforce_apex_language_reference.pdf
Let me know if this is what you needed,
Gaston.
Thank you very much for your response, it helped me.
But now I've got a new problem, when I process the XML , text from a node having accents is not showing correctly in my VF page.
Do you know the reason?
Regards.
Try calling this method :
Encodes a string into the
application/x-www-form-urlencodedformat using a specific encoding scheme, for example “UTF-8.” This method uses the supplied encoding scheme to obtain the bytes for unsafe characters. For more information about the format, see The form-urlencoded Media Type in Hypertext Markup Language - 2.0.
Example:
It encodes the string in utf8, either that or EncodingUtil.urlDecode . Most likely its an encoding issue.
if accents are appearing as á for example, they are html entity encoded. you can safely display them and the browser will show them as the real character.
Hope this helps out, let me know if it solved it
Gaston.
Hi incuGuS,
Sorry for my late response. I left this issue to go on with other projects.
I tested EncodingUtil with no luck.
When printing String s I can see accented characters not showing correctly. They are showing as question marks.
I don't know what to do ...
Regards.
Hard to tell, it could be two things :
The webservice passing the characters in another charset
The webservice printing them incorrectly.
If you see the webservice xml code, do you see the characters correctly?
Im sure its an encoding problem, maybe try UTF8 DECODE instead?
Gaston.
Hi Gaston,
Thank you very much for your help.
The charset used in xml response is ISO-8859-1. The header in the XML file is <?xml version="1.0" encoding="ISO-8859-1"?>
If you call the url http://www.ctt.pt/pdcp/xml_pdcp?incodpos=2000000 from your browser the accents are shown correctly.
Tried with UTF8 DECODE but result is the same.
Regards.
IIRC, getBody() assumes the response is UTF-8.
Thanks for your reply.
Then, if getBody() assumes the response is UTF-8, is there any workaround when response comes in ISO-8859-1?
I tried setting 'Accept-Charset', 'UTF-8' header in the request but had no effect on the response.
Regards.
Do you have any control over the webservice?
Try printing the body in a page with no headers, contentType="text/html" and include inside the whole markup and include the charset as ISO-8859-1.
If you can see the characters correctly then there might be some way, if not , then its trying to encode to utf8 with getBody() as someone suggested and you are LOSING the information, if that happens theres no way back. You might want to contact the webservice provider to allow for a utf8 version , or some support.
I will give it some more thought and let you know if i come up with anything.
Finally I could solve the problem with our project's support contact help.
The class was created with 16.0 API version. Changing to 18.0 fixed the problem.
Thank you very much for your help.
Regards.