1

Scenario: Hello, I have a cURL string which needs to be converted to Http request to be called from apex.

Problem: I am getting 400 Bad Request Error

cURL:

curl https://test.sagepay.com/api/v1/merchant-session-keys \
-u "integrationKey:integrationPassword"  \
-H "Content-type: application/json" \
-X POST \
-d '{ 
    "vendorName": "testVendor" 
}'

Apex Http Request

req.setEndpoint('https://test.sagepay.com/api/v1/merchant-session-keys');

req.setMethod('POST');
req.setHeader('Content-Type','application/json');
req.setBody('vendorName:testVendor');

Blob headerValue = Blob.valueOf(integrationKey +':' +IntegrationPassword);
String authorizationHeader = 'Basic ' + 'EncodingUtil.base64Encode(headerValue);

req.setHeader('Authorization', authorizationHeader);

Note: A similar question has already been posted previously: link here

1
  • Are you sending JSON data to server? Commented Apr 12, 2016 at 5:42

2 Answers 2

2

Try this ;)

req.setBody('{"vendorName":"testVendor"}');

Send valid JSON data to server; As server also saying 400 Bad Request Error;

3
  • This is the correct answer, and it worked. Thanks itzmukeshy7. Also let me know how to mark it as answer, because I cannot add a comment. Mansoor Commented Apr 13, 2016 at 7:48
  • @Mansoor did you log in using the same account ? Commented Apr 13, 2016 at 12:55
  • @Mansoor You simply need to mark an answer as correct (the green check image). Find more here Accept answer Commented Apr 13, 2016 at 12:58
1

Try to add this line in your code.

req.setBody('vendorName=testVendor');

Instead of:

req.setBody('vendorName:testVendor');
0

You must log in to answer this question.

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