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.

  • 1
    what error are you getting? – glls Mar 8 '19 at 19:35
  • Possible duplicate of How to make a post request to some other server from apex controller – glls Mar 8 '19 at 19:36
  • @glls That is not an appropriate duplicate, as far as I can tell. – sfdcfox Mar 8 '19 at 19:37
  • not quite sure what the op's issue is as is. ill wait a bit for an update – glls Mar 8 '19 at 19:39
  • @glls application/json vs application/x-www-form-encoded, as far as I can tell. – sfdcfox Mar 8 '19 at 19:45
3

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);
| improve this answer | |
  • 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 '19 at 20:21
  • @Sid Yes, you need to do it just like I posted in my answer. – sfdcfox Mar 8 '19 at 20:25
  • @Sid Yeah, it should have been length, not size. – sfdcfox Mar 8 '19 at 20:26
  • You are brilliant @sfdcfox – Sid Mar 8 '19 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 '19 at 21:28

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