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

  • Are you sending JSON data to server? – itzmukeshy7 Apr 12 '16 at 5:42
2

Try this ;)

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

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

|improve this answer|||||
  • 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 – Mansoor Ahmad Apr 13 '16 at 7:48
  • @Mansoor did you log in using the same account ? – Samuel De Rycke Apr 13 '16 at 12:55
  • @Mansoor You simply need to mark an answer as correct (the green check image). Find more here Accept answer – itzmukeshy7 Apr 13 '16 at 12:58
1

Try to add this line in your code.

req.setBody('vendorName=testVendor');

Instead of:

req.setBody('vendorName:testVendor');
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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