0

I am constructing something wrong, in the POST request. Please advise.

Step 1: Created Named Credential called Stock_Check_For_Materials

Step 2: Created Apex Controller as below

Continuation con = new Continuation(40);
con.continuationMethod='processResponse';
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:Stock_Check_For_Materials/stockcheck');
req.setMethod('POST');
req.setHeader('Content-Type','application/json');  
req.setBody('customerNumber='+EncodingUtil.urlEncode(cN, 'UTF-8')+'&requireGenericEquivalent='+EncodingUtil.urlEncode(rGE, 'UTF-8')+'&itemNumber='+EncodingUtil.urlEncode(itN, 'UTF-8')+'&orderQuantity='+EncodingUtil.urlEncode(oQ, 'UTF-8'));
this.requestLabel = con.addHttpRequest(req);

Please note that the 'customerNumber', 'requireGenericEquivalent', 'itemNumber' and 'orderQuantity' are the POST body for request and cN,rGE,itN,oQ are the getter and setter variable values coming from the visualforce page to this controller.

closed as off-topic by glls, battery.cord, Pranay Jaiswal, Reshma, Mohith Shrivastava Mar 12 at 17:51

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions on problems in code you've written must describe the specific problem and include valid code to reproduce it. For help writing short, self-contained syntactically-valid examples, see: SSCCE.org" – battery.cord, Pranay Jaiswal, Reshma, Mohith Shrivastava
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    what error are you getting? – glls Mar 8 at 19:35
  • @glls That is not an appropriate duplicate, as far as I can tell. – sfdcfox Mar 8 at 19:37
  • not quite sure what the op's issue is as is. ill wait a bit for an update – glls Mar 8 at 19:39
  • @glls application/json vs application/x-www-form-encoded, as far as I can tell. – sfdcfox Mar 8 at 19:45
2

You've specified the Content-Type as application/json, but you're using a application/x-www-form-encoded encoding style. You need to make sure they agree with each other.

// These do not agree with each other
req.setHeader('Content-Type','application/json');
req.setBody('customerNumber='+EncodingUtil.urlEncode(cN, 'UTF-8')+'&requireGenericEquivalent='+EncodingUtil.urlEncode(rGE, 'UTF-8')+'&itemNumber='+EncodingUtil.urlEncode(itN, 'UTF-8')+'&orderQuantity='+EncodingUtil.urlEncode(oQ, 'UTF-8'));

Corrected

String jsonBody = JSON.serialize(
new Map<String, String> {
'customerNumber' => cN,
'requireGenericEquivalent' => rGE, 
'itemNumber' => itN,
'orderQuantity' => oQ});
req.setHeader('Content-Type','application/json');
req.setHeader('Content-Length',jsonBody.length()+'');
req.setBody(jsonBody);
  • Thanks @sfdcfix -> Method does not exist or incorrect signature: void size() from the type String at line 29 column 49 (This is the second last line) – Sid Mar 8 at 20:21
  • @Sid Yes, you need to do it just like I posted in my answer. – sfdcfox Mar 8 at 20:25
  • @Sid Yeah, it should have been length, not size. – sfdcfox Mar 8 at 20:26
  • You are brilliant @sfdcfox – Sid Mar 8 at 20:28
  • Need 1 more help, my endpoint is expecting JSON in this format { "stock": { "accountInfo": { "customerNumber": "121", "requireGenericEquivalent": "Y" }, "itemInfo": { "itemNumber": "121", "orderQuantity": "10" } } } What will be the right jsonBody please ? – Sid Mar 8 at 21:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.