2

I'm working with a third party vendor and they send notifications when forms are submitted in their system to RestResource in Salesforce. Problem I'm having is to receive JSON over XML I have to accept JSON in the header, but I'm not sure if that is possible (or I'm doing something wrong or there is something wrong on their end).

I have class I'm using to deserialize the JSON into a class and I have tested in Workbench to verify.

@RestResource(urlMapping='/formreceipt')
global class REST_FormReceipt { 
     @HttpPost
     global static void formReceipt() {
         RestContext.request.addHeader('Accept', 'application/json');       
         FormReceipt data = FormReceipt.parse(RestContext.request.requestBody.toString());
     }
 }

Would I have to setup my RestResource method differently?

Andy

11
  • What's the issue here ? Any errors Aug 5, 2016 at 0:05
  • Why do you have to have that header in order to process the JSON?
    – Eric
    Aug 5, 2016 at 0:05
  • 1
    Adding accept type should make you accept only JSON .Change to xml in workbench and test it Aug 5, 2016 at 0:06
  • 1
    But it does not. Setting the Accept for the request in the apex rest class does nothing. Workbench still happily sends JSON when the accept is changed to XML in the apex rest. Makes no sense to do it this way as by the time you change the accept the request is already received by the rest service. Eailsy proven if you replicate the OP code and send JSON from WB when changing the accept in the apex rest to XML
    – Eric
    Aug 5, 2016 at 3:46
  • Your parser class assumes well-formed JSON... yes? It is your web service, you dictate the terms! Why don't you insist on well-formed JSON? And return 4xx ("your bad, client") if you cannot make heads or tails of the request body. MUST YOU wrap JSON inside XML? That just seems so... wrong. Am I reading your question correctly where you state "... to receive JSON over XML"?
    – krigi
    Aug 5, 2016 at 4:51

1 Answer 1

2

This:

RestContext.request.addHeader('Accept', 'application/json'); 

makes sense when you are making a request from Salesforce to an external system; you are saying that you would like JSON to be returned to you. (Perhaps you make such calls elsewhere and if so you should set this header before you make your calls.)

In your code sample, some data has been sent to Salesforce so its is too late to try to negotiate the format of that data.

If the external system is already sending JSON then you have nothing to do. If it is sending XML then there may be some way to pre-register your preference or you may just be stuck having to parse the XML which is fairly straightforward using the DOM class.

1
  • Thanks Keith. Waiting back from MemberClicks to see if they can send the data in JSON. I'm familiar with the DOM class, so it will be just a bit more code to use that over JSON.
    – Andrew L.
    Aug 5, 2016 at 14:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .