1

Here is my code for callout.

        Http http = new Http();
        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();

        req.setEndpoint('https://google.com?q=');
        req.setHeader('Content-Type','x-www-form-urlencoded');
        req.setHeader('Content-Length', '0'); 
        req.setTimeOut(1200);
        req.setMethod('POST');
        req.setBody(account.name);

        res = http.send(req);
        system.debug(res.toString());

This is working for other test urls like posttestserver.com but for Google.com, getting response as "Status=Method Not Allowed, StatusCode=405". Added http://google.com in remote site settings.

Thanks.

2
  • 2
    what google service are you trying to post to? Simply posting to google is not allowed
    – Eric
    Commented Oct 17, 2015 at 19:25
  • Hi Eric. I am trying to do a search with account.name in google and return the response.
    – sf_user
    Commented Oct 17, 2015 at 19:29

1 Answer 1

3

You can't post data directly to google so you need to get data using API.

        Http http = new Http();
    HttpRequest req = new HttpRequest();
    HttpResponse res = new HttpResponse();

    req.setEndpoint('https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=' + Account.Name);
    req.setHeader('Content-Type','x-www-form-urlencoded');
    req.setHeader('Content-Length', '0'); 
    req.setTimeOut(1200);
    req.setMethod('GET');
    res = http.send(req);
    system.debug(res.getbody());

It will return data in Json format as shown below

enter image description here

4

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .