1

I have a wierd problem: I'm calling SAP from salesforce apex and I have trouble with one of my request.

This one is working.

    request.setEndpoint(authentification_details.Webservice_enpoint__c+'/v1/product/ProductMasterSet(product=\''+productCode+ '\')?$format=json');

But the next one is not working

request.setEndpoint(authentification_details.Webservice_enpoint__c+'/v1/product/ProductsSet?$filter=Productdescription eq \'XXXXXX\' and Language eq \'EN\'');

(XXXX is just a product code). I got a 400 code and the call never reach the SAP side. I played around a bit and think i figured out that the '$' sign is possibly responsible for the error, because when i test in postman this is working.

EDIT: it was the spaces responsible for the errors (see answer below)

I did try encoding the $ sign using exa (%24) and unicode ($) Exa is not reaching SAP either but unicode is but is not interpreted correctly on SAP Side.

Does anyone has had this problem before? is there a way to force the encoding of my request so it's like postman?

Thanks in advance

3
  • 1
    If I had to guess, the issue is likely not the dollar sign, but the query syntax in the URL since it has spaces. You may want to try encoding the spaces %20 or using + signs instead since spaces in URLs can be problematic. It could also be the single quotes (Apex has to handle the string with escaped characters first before it's ferried off to the HTTP service which can then re-encode) but you already have a working example with single quotes.
    – nbrown
    Commented Nov 22, 2023 at 15:35
  • 1
    You can try sending the query string syntax to Postman's Echo API via Apex to see how the request gets encoded/changed.
    – nbrown
    Commented Nov 22, 2023 at 15:36
  • @nbrown FWIW my first guess was the quotes too but they seem to be ok!
    – Matt Lacey
    Commented Nov 22, 2023 at 15:45

1 Answer 1

4

The $ doesn't seem to cause any issues in a quick test I just performed. I created an endpoint at beeceptor.com, added a remote site setting and then ran this code via execute anonymous in my scratch org:

HttpRequest req = new HttpRequest();
String productCode = 'bob1234';
req.setEndpoint('https://quotes.free.beeceptor.com/v1/product/ProductMasterSet(product=\''+productCode+ '\')?$format=json');
req.setMethod('POST');
HttpResponse res = new Http().send(req);

Screenshot from beeceptor.com showing the request has been received ok

For the second request, the spaces appear to cause a 400 error. I also ran this code:

HttpRequest req = new HttpRequest();
String productCode = 'bob1234';
// req.setEndpoint('/v1/product/ProductMasterSet(product=\''+productCode+ '\')?$format=json');
String url = 'https://quotes.free.beeceptor.com/v1/product/ProductsSet?$filter=Productdescription eq \'XXXXXX\' and Language eq \'EN\'';
req.setEndpoint(url);
req.setMethod('POST');
HttpResponse res = new Http().send(req);
System.Debug(res);

Which gave me this in the log:

07:36:02.44 (238032455)|USER_DEBUG|[7]|DEBUG|System.HttpResponse[Status=Bad Request, StatusCode=400]

And then modified it to encode the spaces:

HttpRequest req = new HttpRequest();
String productCode = 'bob1234';
String url = 'https://quotes.free.beeceptor.com/v1/product/ProductsSet?$filter=Productdescription eq \'XXXXXX\' and Language eq \'EN\'';
url = url.replaceAll(' ', '%20');
req.setEndpoint(url);
req.setMethod('POST');
HttpResponse res = new Http().send(req);
System.Debug(res);

And once again, this one got sent successfully. If SAP won't accept the URL with encoding then you might have some trickier issues to resolve!

Screenshot from beeceptor.com showing the request with encoded spaces was receied ok

3
  • I'm sorry, it's the second request that is not working not the first one I edited my question as it wasn't clear. Sorry about that. Thanks for the answer. Commented Nov 22, 2023 at 15:24
  • @JulienSouchi I misread your question - my bad! Seems like the spaces are the issue, see my edits.
    – Matt Lacey
    Commented Nov 22, 2023 at 15:42
  • Thanks for the update, that was the spaces indeed. I'm getting to SAP now. Thanks again Commented Nov 23, 2023 at 9:26

You must log in to answer this question.

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