Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a cURL command to http request in angular?

I have this cURL command from paypal developers page:

https://developer.paypal.com/docs/api/get-an-access-token-curl/

curl -v https://api.sandbox.paypal.com/v1/oauth2/token \
   -H "Accept: application/json" \
   -H "Accept-Language: en_US" \
   -u "client_id:secret" \
   -d "grant_type=client_credentials"

My question is, how can i convert that cURL command into a http request with angular?

Can you give me an example of how can i do that? Because i am new with angular and i need help with this..

Thanks!

like image 491
Luis Bermúdez Avatar asked Oct 27 '25 14:10

Luis Bermúdez


1 Answers

The first thing I would do is to figure out what you actually need to make the request. Looks like you have some auth headers that need to be passed and I'm assuming it's a POST request.

You could simplify it by using something like a CURL to Http Request tool to get it into a more familiar format.

TL;DR: Here you go:

    const headers = new HttpHeaders({
      'Accept': 'application/json',
      'Accept-Language': 'en_US',
      'Content-Type': 'application/x-www-form-urlencoded',
      'Authorization': 'Basic Y2xpZW50X2lkOnNlY3JldA=='
    });
    this.http.post('https://api.sandbox.paypal.com/v1/oauth2/token', 'grant_type=client_credentials', { headers: headers });

this.http would be the reference to your injected HttpClient

Here is a stackblitz. The request turns a 401 so I'm assuming the creds are invalid.

like image 116
mwilson Avatar answered Oct 30 '25 05:10

mwilson