Hi i write a basic web service in apex on salesforce.com plateform here is apex code
@RestResource(urlMapping='/gggg')
global with sharing class HelloWorld{
@HttpPost
global static String doPut(){
RestRequest req=RestContext.request;
RestResponse res=RestContext.response;
String body1=req.requestBody.toString();
return body1;
}
}
and i am trying to send a post request to this service using android
first i authorize it and then get access token and refresh token and after that i again get access_token so there is no problem in access token.here is code for android client
URL url1=new URL("https://ap1.salesforce.com/services/apexrest/gggg");
HttpURLConnection connection=(HttpURLConnection) url1.openConnection();
connection.addRequestProperty("Authorization", "OAuth "+str);
connection.setRequestMethod("POST");
connection.setDoOutput(true);
String jsonbody="[{type:QwikScore_Question_Answer__c ,id:a03900000034MfA ,field:{id:Answer_Text__c,value:1.0}}]";
OutputStream st=connection.getOutputStream();
st.write(jsonbody.getBytes());
st.close();
connection.connect();
BufferedReader reader=new BufferedReader(new InputStreamReader(connection.getInputStream()));
String ss="";
String ss1="";
while((ss=reader.readLine())!=null){
ss1+=ss;
}
Log.i("Hello world",ss1);
in output response in Logcat for Hello World i am getting “” as output please point me why i am not getting correct output.i am expecting
[{type:QwikScore_Question_Answer__c ,id:a03900000034MfA ,field:{id:Answer_Text__c,value:1.0}}]
as output because i send
st.write(jsonbody.getBytes());
jsonbody as body of the request.please help me how to get correct output??
In your request you never set the content-type header, so the server doesn’t know how to parse the payload.