You need to sign in to do that
Don't have an account?
David Roberts 4
No Protocol Error for HttpRequest JSON POST
The following code throws an exeption at the callout request
CALLOUT_REQUEST [35]|System.HttpRequest[Endpoint=http//:www.virtualworlds.co.uk/getParams.php, Method=POST]
and genearates the error,
"no protocol: http//:www.virtualworlds.co.uk/getParams.php".
I generate the list from custom objects and have verified that the list has entries.
Can you tell me what is wrong?
Code:
public class HelloWorld {
public static void tryPost() {
System.debug( 'Posting...' );
List<licence__c> myList = new List<licence__c>();
myList= [select Id,Name from licence__c LIMIT 20];
String endPoint = 'http//:www.virtualworlds.co.uk/getParams.php';
String method = 'POST';
//Prepare http req
System.debug( 'New Request...' );
HttpRequest req = new HttpRequest();
System.debug( 'Set endpoint...' );
req.setEndpoint(endPoint);
req.setMethod(method);
String JSONString = JSON.serialize(myList);
req.setBody(JSONString);
System.debug( 'new Http...' );
Http http = new Http();
System.debug( 'Send request...' );
HTTPResponse res = http.send(req);
System.debug( 'Result?...' );
System.debug(res.getBody());
}
}
CALLOUT_REQUEST [35]|System.HttpRequest[Endpoint=http//:www.virtualworlds.co.uk/getParams.php, Method=POST]
and genearates the error,
"no protocol: http//:www.virtualworlds.co.uk/getParams.php".
I generate the list from custom objects and have verified that the list has entries.
Can you tell me what is wrong?
Code:
public class HelloWorld {
public static void tryPost() {
System.debug( 'Posting...' );
List<licence__c> myList = new List<licence__c>();
myList= [select Id,Name from licence__c LIMIT 20];
String endPoint = 'http//:www.virtualworlds.co.uk/getParams.php';
String method = 'POST';
//Prepare http req
System.debug( 'New Request...' );
HttpRequest req = new HttpRequest();
System.debug( 'Set endpoint...' );
req.setEndpoint(endPoint);
req.setMethod(method);
String JSONString = JSON.serialize(myList);
req.setBody(JSONString);
System.debug( 'new Http...' );
Http http = new Http();
System.debug( 'Send request...' );
HTTPResponse res = http.send(req);
System.debug( 'Result?...' );
System.debug(res.getBody());
}
}
Hopefully this will help others - a simple post method:
public class TestPost {
public static void tryPost() {
String payLoad = 'some_data=value+1&some_more_data=value+2&etc_etc';
Http h = new Http();
HttpRequest req = new HttpRequest();
//enter your URL
req.setEndpoint('http://www.XYZ.com/getParams.php');
req.setMethod('POST');
req.setBody(payLoad);
HttpResponse res = h.send(req);
system.debug('Res=' + res);
} //end tryPost
} //end class TestPost