0

I have a curl request which I have to convert to a rest request in Apex. This is the request:

   curl https://api.stripe.com/v1/subscriptions \
  -u sk_test_4eC39HqLyjWDarjtT1zdp7dc: \
  -d customer=customer_id\
  -G

And this is the code I used:

HttpRequest req1 = new HttpRequest();
req1.setMethod('GET');
req1.setEndpoint('https://api.stripe.com/v1/subscriptions');

String urlBody = 'customer='+ customerId;
String username = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc';
Blob headerValue = Blob.valueOf(username);
String authorizationHeader = 'Bearer ' + username;
req1.setHeader('Authorization', authorizationHeader);

req1.setBody(urlBody);
Http binding1 = new Http();
HttpResponse response1 = binding1.send(req1);

I have converted many curl to rest in the same way. But, this is the only cURL thats not working. This is the response I am getting:

 {
  "error": {
    "code": "parameter_missing",
    "doc_url": "https://stripe.com/docs/error-codes/parameter-missing",
    "message": "Missing required param: items.",
    "param": "items",
    "type": "invalid_request_error"
  }
}

I think the only difference that this cURL has is there is an extra -G at the end of the cURL. I think thats creating the problem. Also, I have run the cURL on the Postman and its returning data fine. So, whats wrong with my converting?

  • are you trying to retrieve the subscription? – User6670 May 19 at 10:10
  • @User6670 yes. i am. – Jayesh Babu May 19 at 10:10
  • in the documentation of stripe i see the following curl to retrieve the subscription curl api.stripe.com/v1/subscriptions/sub_HJ3fAVru2hvtxY \ -u sk_test_4eC39HqLyjWDarjtT1zdp7dc: – User6670 May 19 at 10:14
  • they dont mention customerid in there just the subscriptionid – User6670 May 19 at 10:15
  • yeah.. but i dont know the subscription id. i only know customer id. thats why i am using the above request. – Jayesh Babu May 19 at 10:17
2

The following api can get you customer subscription

HttpRequest req1 = new HttpRequest();
req1.setMethod('GET');
String customerId='cus_HJ3f4cSLOxY9a6';
string endpoint='https://api.stripe.com/v1/customers/'+customerId+'/subscriptions';
req1.setEndpoint(endpoint);

String username = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc';
Blob headerValue = Blob.valueOf(username);
String authorizationHeader = 'Bearer ' + username;
req1.setHeader('Authorization', authorizationHeader);

Http binding1 = new Http();
HttpResponse response1 = binding1.send(req1);
system.debug(response1);
system.debug(response1.getBody());
| 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.