+ Start a Discussion
Platy ITPlaty IT 

Can't see parameters in Rest HTTPPost Method

I'm sure I'm missing something obvious here.  I've developed a @RestResource class with an @HttpPost method.  Here's the method-

@HttpPost
	global static String ImportContact() {
		RestRequest req = RestContext.request;
		System.debug('req is---------------------- ' +req);
		RestResponse resp = RestContext.response;
		
		
		Contact impContact = new Contact();
		impContact.FirstName = req.params.get('firstname');
		impContact.LastName = req.params.get('lastname');
		impContact.FirstName = req.params.get('email');
		impContact.Website_Id__c = req.params.get('userid');
		impContact.Phone = req.params.get('phone');
		impContact.LinkedIn_Profile_URL__c = req.params.get('linkedin');
		impContact.Facebook_Profile_URL__c = req.params.get('facebook');
		impContact.Website__c = req.params.get('personalwebsite');
		impContact.Twitter__c = req.params.get('twitter');
		try{
			impContact.Startup_Administrator__c = Boolean.valueOf(req.params.get('startupadmin'));
		} Catch (Exception e){
			resp.statusCode = 400;
			return 'startupadmin parameter must equal true or false';
		}
		
		//Check for a matching Contact
		
		
		try{
			upsert impContact;
		} Catch (DMLException e){
			resp.statusCode = 400;
			return 'Error loading Contact into Salesforce- ' +e.getMessage(); 
		}
		
		/*Blob pic = req.requestBody;
		if (pic.size() > 0){
			
		}*/
		
		resp.statusCode = 201;
		return 'Contact created';
		
	}

 Using curl, I can connect to this and pass in a file of JSON parameters as below, but all of those req.params.get calls in the method return null so the post returns an error?  

{
"firstname": "Json",
"lastname": "test",
"email": "email@email.com",
"phone": "914-417-9780",
"startupadmin": "true"
}

 

What confuses me is that in the debug log, I examine the RestRequest and I see the parameter values there-

10:39:21.061 (61510000)|USER_DEBUG|[12]|DEBUG|req is---------------------- RestRequest:[headers={Accept=*/*, CipherSuite=RC4-MD5 TLSv1 128-bits, Content-Type=application/x-www-form-urlencoded, Host=cs14.salesforce.com, User-Agent=curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8r zlib/1.2.5, X-Salesforce-SIP=108.6.201.115}, httpMethod=POST, params={{"firstname": "Json","lastname": "test","email": "email@email.com","phone": "914-417-9780","startupadmin": "true"}=}, remoteAddress=108.6.201.115, requestBody=Blob[0], requestURI=/NewEntStartup/, resourcePath=/services/apexrest/NewEntStartup/*]

 

Anyone seeing what I'm clearly missing here?  

 

Best Answer chosen by Admin (Salesforce Developers) 
Platy ITPlaty IT

Sorted this out, seems this was my misunderstanding of what the params are.  I was hoping to pass in both JSON parameters and a binary file in the same request but it doesn't look like that's possible, I'll just split it to another method.

All Answers

Platy ITPlaty IT

One step forward, two steps back.  I noticed the Content-type of my request was not JSON (I thought that was the default).  So I added that into my request header and now my params in the RestRequest are completely empty.  Any suggestions would be greatly appreciated.

 

If it helps, here's the curl command I'm using, just in case the issue is there-

curl https://cs14.salesforce.com/services/apexrest/NewEntStartup/ -H 'Content-Type: application/json' -H 'Authorization: OAuth token' -d @entstartupdata.json

Platy ITPlaty IT

Sorted this out, seems this was my misunderstanding of what the params are.  I was hoping to pass in both JSON parameters and a binary file in the same request but it doesn't look like that's possible, I'll just split it to another method.

This was selected as the best answer
VentilsVentils
Could you please provide the code as to how you accomplished it, It will be helpful for me as well as others.

Regards
Vent