Reputation: 322
I have created a rest resource in apex and I am passing in an id through the body through post man via GET request. I then deserialize the body and I am getting an empty string even when I am providing the id. code below.
// rest resource code, More to code but I think this is the important stuff
String res = RestContext.Request.requestBody.toString();
if(res == ''){
response.statusCode = 400;
return;
}
HogtieDTO.getDeviceRequestInfoResponse resData = (
HogtieDTO.getDeviceRequestInfoResponse)JSON.deserialize(
RestContext.Request.requestBody.toString(),
HogtieDTO.getDeviceRequestInfoResponse.class
);
//HogtieDTO class
public with sharing class HogtieDTO {
public class getDeviceRequestInfoResponse {
public String customerId;
}
}
I get the 401 after i make this call.
I can get it to work by passing it via a param but Im trying to get it to work through the body.
Upvotes: 0
Views: 8173
Reputation: 19637
More of a comment than answer but it'd be too long.
passing in an id through the body through post man via GET request
If it's a GET request - Postman should ignore the body, GETs use only the url/uri/whatever is the official name. Make sure it's a POST and that your function is also annotated something like @HttpPost @ReadOnly global static String doPost()
I get the 401
401 is Unauthorized. Are you sure you passed Authorization Bearer <session id / access token goes here>
header? Was the error response just a header or with body too? Is your user's profile allowed to access that class and the connected app you've used has api access scope?
401 would kick in even before your code has a chance to run, it's not even on the list (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_rest_methods.htm scroll to bottom). Are you sure it runs? Do you see anything in debug logs?
Also, If someone could answer when you would have an Id in the path vs body that would be awesome
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_rest_code_sample_basic.htm contains a sample method annotated @httpget
and parses account id from the request's /services/apexrest/Account/001...
with req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
Upvotes: 1