You need to sign in to do that
Don't have an account?
Eric Blaxton 11
APEX callout HTTPRequest error
Hi and thanks for any tips in advance.
First time writing a Get request.
1. My Class:
3. My anonymous apex code:
4. Response body (system.debug):
5. The error:
Regards,
Eric
First time writing a Get request.
1. My Class:
public class MaintenanceTicket { public static String getMaintenanceTickets(string location ) { HTTP h = new HTTP(); HTTPRequest req = new HTTPRequest(); req.setEndpoint('https://api.fake/odata/AccountBalances?$expand=businessUnit&$count=true&$skip=0&$top=3'); req.setTimeout(120000); req.setMethod('GET'); // QA Authorization req.setHeader('Authorization', 'Bearer ' + 'FakeID'); HTTPResponse res = h.send(req); String strResponse = res.getBody(); if(res.getstatusCode() == 200 && res.getbody() != null) system.debug('Response: ' + res.getBody()); { //MAP<String,Object> jsonBody = (Map<String,Object>)Json.deserializeUntyped(res.getBody()); // pass data List<Object > jsonBody = (List<Object>) Json.deserialize(strResponse, List<Object>.class); system.debug('Got these values:'); System.debug(jsonBody); String ticket = String.valueof(jsonBody); return ticket; } } }2. My Remote Site settings is accurate
3. My anonymous apex code:
String busloc = MaintenanceTicket.getMaintenanceTickets('385'); System.debug(busloc);
4. Response body (system.debug):
13:10:22:633 USER_DEBUG [26]|DEBUG|Response: {"@odata.context":"http://api.fake/$metadata#AccountBalances(businessUnit())", "@odata.count":xxx, "value":[{"id":1, "businessUnitId":xxx, "charges":46536.710000, "credits":-37741.810000, "balance":8794.900000, "effectiveDate":"2020-03-03T02:54:03.81-06:00", "businessUnit":{"id":385,"transactionType":2,"billToNumber":"
5. The error:
13:10:22:654 FATAL_ERROR System.JSONException: Malformed JSON: Expected '[' at the beginning of List/Set 13:10:22:000 FATAL_ERROR Class.System.JSON.deserialize: line 15, column 1 13:10:22:000 FATAL_ERROR Class.MaintenanceTicket.getMaintenanceTickets: line 30, column 1
Regards,
Eric
Hello Eric,
So the issue seems to be that since you are trying to deserialize the JSON into a List<>, which should start with an '[', it fails, since your response: strResponse is starting with an "{" => {"@odata.context"
If you would have taken only the value (shown below) from the object
List<Object> isn't an JSON supported Apex type, so it would have failed, giving you another error.Also values like below would fail, since xxx would have to be a string still even the deserialization of the JSON (or a number).
I am guessing that you want the information within the "value" property of the response so I would probably do something like this:
All Answers
Check this once.
I've read this. What point are you trying to make?
Eric
Hello Eric,
So the issue seems to be that since you are trying to deserialize the JSON into a List<>, which should start with an '[', it fails, since your response: strResponse is starting with an "{" => {"@odata.context"
If you would have taken only the value (shown below) from the object
List<Object> isn't an JSON supported Apex type, so it would have failed, giving you another error.Also values like below would fail, since xxx would have to be a string still even the deserialization of the JSON (or a number).
I am guessing that you want the information within the "value" property of the response so I would probably do something like this: