+ Start a Discussion
Vishalg89Vishalg89 

read rhe JSON from POST URL

Hi all,

 

I need to create a REST service URL  in apex, through which a PHP application POST a JSON object to me and then I need to read the JSON from that. please give any help about this.

 

My sample code:

 

@RestResource(urlMapping='/myapp/')
global with sharing class myController{

@HttpPOST
/* This is method is responsible for reading the JSON data*/
global static String doPOST()
{

       ////////////////////// read JSON /////////////////////////////

}

 

please help me regarding this problem.

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox
global static String doPOST() {
  RestRequest req = RestContext.request;
  MyWrapperClass mwc = (MyWrapperClass)JSON.deserialize(req.requestBody,MyWrapperClass.class);
   // mwc now has my data from PHP.
}

MyWrapperClass is just some generic class that contains the fields you are receiving from the PHP script. You could also use the JSONParser object as well, or deserialize into a map, etc. You should probably read the documentation for JSON parsing so you have a better idea of how you'd like to structure your code.

All Answers

gbu.varungbu.varun

Hi Vishal, If you are able to get URL through REST API you should use Json Parser Method to read data.

Vishalg89Vishalg89

But how to read JSON object from URL? another application hitting my URL and POST the JSON object? I dont know, how to read that?

sfdcfoxsfdcfox
global static String doPOST() {
  RestRequest req = RestContext.request;
  MyWrapperClass mwc = (MyWrapperClass)JSON.deserialize(req.requestBody,MyWrapperClass.class);
   // mwc now has my data from PHP.
}

MyWrapperClass is just some generic class that contains the fields you are receiving from the PHP script. You could also use the JSONParser object as well, or deserialize into a map, etc. You should probably read the documentation for JSON parsing so you have a better idea of how you'd like to structure your code.

This was selected as the best answer
Vishalg89Vishalg89

thanks very much...

Vishalg89Vishalg89

I applied the code as you suggested. I am posting JSON object from PHP code and but i am not getting anything in "req.requestBody" It is empty.

gbu.varungbu.varun

Hi

You can pass url by EndPoint.

You will receive input through HTTPResponse. Try my running code.

HttpRequest req = new HttpRequest();
Http http = new Http();
HTTPResponse res;
try{
	req.setEndPoint(URL);
	req.setMethod('POST');
	res = http.send(req);
	String s = res.getBody();
	JSONParser parser = JSON.createParser(s);
	while (parser.nextToken() != null) {
		if(parser.getCurrentToken() == JSONToken.FIELD_NAME)
			System.debug(parser.getCurrentName());
		else{
			System.debug(parser.getText());
		}
	}
}
catch(Exception e)
{}

 

Vishalg89Vishalg89

thanks Varun, but looking into your code, it seems that you r hitting the URL of another application.. but in my case, a PHP application hittinf my REST API URL and POST a JSON to me and I am not able to read that!!!!

gbu.varungbu.varun

If you are getting JSON you should go through this link 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_jsonparser.htm

 

HTTPResponse 
Vishalg89Vishalg89

thanks to all.. solution code worked for me...