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 Oct 17 '15 at 19:25
  • Hi Eric. I am trying to do a search with account.name in google and return the response. – sf_user Oct 17 '15 at 19:29
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.