0

I'm trying to make a callout to a Stripe Api with Apex. I made the exactly same request in Postman with the same Http configuration and have this working well. But when running it with Apex i get a Http 400 (Bad Request) with this error message:


{
  "error": {
    "message": "This property cannot be expanded (data).",
    "type": "invalid_request_error"
  }
}

What I want to do is to query a list of Payment Intents from stripe and expand the balance transaction stored in the payment charge data. And here is how I do it


Http http = new Http();

HttpRequest request = new HttpRequest(); 
request.setHeader('Authorization', 'Bearer Token');  
request.setHeader('Content-Type', 'application/x-www-form-urlencoded');

String payload = 'expand[]=data.charges.data.balance_transaction';

request.setMethod('GET');

request.setEndpoint(API_ENDPOINT + '/v1/payment_intents');

request.setBody(payload);

HttpResponse response = http.send(request);

System.debug(response.getBody());

Can anyone help me please to understand what I am missing here?

6
  • 1
    Are you actually appending the token string to the Authorization header, request.setHeader('Authorization', 'Bearer '+TokenVARIABLE);? Apr 24, 2021 at 15:03
  • @gale-force-wizard This is not how i did it in my code. I wrote the authorization like that just for readers to have the different steps of my approach. I have no authorization problem and all requests without requestBody works. After testing a lot of time i think the problem is in my way of defining body for my stripe requests. But i still do not have find a way to do that well and make it work. Apr 24, 2021 at 17:37
  • Salesforce doesn't support GET with body. That's why they're failing, those requests are actually sent as POST which the external system doesn't recognise. Apr 25, 2021 at 13:42
  • @SachinHooda when I make a GET on a single payment_intent with expand[]=charges.data.balance_transaction as body I have the result and my balance_transaction is expanded as wished. And my logs also tell me that the callout made is a GET. But if Salesforce really change my request Method do you know a way to make a GET to an API wanted us to give a body to the request? Thanks Apr 25, 2021 at 14:32
  • @SachinHooda I think you were right. Make the same request on postman with POST method give me the same errors. So is there any way to byparse that or that is just impossible with saleforces? Apr 25, 2021 at 14:39

1 Answer 1

-1

You need to set the Authorization token correctly. sToken (SessionID)

request.setHeader('Authorization', 'Bearer ' + sToken);

Also could you check your payload ? I believe you are able to make the call but the payload is incorrect.

1
  • Yes I have authorization and my callouts are working since the error message I get come from stripe and is not a 401(Unauthorized) meaning that I correctly reached my stripe data source. I think you are right about the payload but I still do not have find a way to make it work with Apex. When I make a query on a single payment_intent with expand[]=charges.data.balance_transaction as payload I have the result and my balance_transaction is expanded as wished. So I don't understand why the same logic for the payload with a list of payment_intent is not working but work when testing with postman Apr 25, 2021 at 12:49

You must log in to answer this question.

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