+ Start a Discussion
fer_farfer_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.

Best Answer chosen by Admin (Salesforce Developers) 
incuGuSincuGuS

 

public class HttpCalloutSample {
    // Pass in the endpoint to be used using the string url
   
    public String getContent(String url) {
        // Instantiate a new http object
        Http h = new Http();
        
        // Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
        HttpRequest req = new HttpRequest();
        req.setEndpoint(url);
        req.setMethod('GET');
        
        // Send the request, and return a response
        HttpResponse res = h.send(req);
        return res.getBody();
    }
}

 

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

incuGuSincuGuS

 

public class HttpCalloutSample {
    // Pass in the endpoint to be used using the string url
   
    public String getContent(String url) {
        // Instantiate a new http object
        Http h = new Http();
        
        // Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
        HttpRequest req = new HttpRequest();
        req.setEndpoint(url);
        req.setMethod('GET');
        
        // Send the request, and return a response
        HttpResponse res = h.send(req);
        return res.getBody();
    }
}

 

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.

This was selected as the best answer
fer_farfer_far

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.

incuGuSincuGuS

Try calling this method :

 

Encodes a string into the

application/x-www-form-urlencoded

format 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:

 

String encoded = EncodingUtil.urlEncode(url, 'UTF-8');

 

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.

 

fer_farfer_far

Hi incuGuS,

 

Sorry for my late response. I left this issue to go on with other projects.

 

I tested EncodingUtil with no luck.

 

 

        HttpRequest req = new HttpRequest();
        req.setEndpoint(url);
        req.setHeader('Content-type', 'text/xml');
        req.setHeader('Accept-Charset', 'UTF-8');
        req.setMethod('GET');

        HttpResponse res = h.send(req);
        String s = EncodingUtil.urlEncode(res.getBody(), 'UTF-8'));

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.

 

incuGuSincuGuS

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.

fer_farfer_far

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.

SuperfellSuperfell

IIRC, getBody() assumes the response is UTF-8.

fer_farfer_far

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.

incuGuSincuGuS

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.

 

 

<meta content="text/html; charset="iso-8859-1" http-equiv="Content-Type" />

 

 

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.

fer_farfer_far

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.

タン ビン チャンタン ビン チャン
// Instantiate a new http object
        Http h = new Http();
        
        // Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
        HttpRequest req = new HttpRequest();
        req.setEndpoint('https://alivecoupon.com');
        req.setMethod('GET');
        
        // Send the request, and return a response
        HttpResponse res = h.send(req);
        System.debug( res.getBody());