1

How can I convert the following curl request into HTTP post request in angular 7?

curl --request POST \
  --url https://api.typeform.com/oauth/token \
  --data-urlencode 'grant_type=authorization_code' \
  --data-urlencode 'code={temporary_authorization_code}' \
  --data-urlencode 'client_id={your_client_id}' \
  --data-urlencode 'client_secret={your_client_secret}' \
  --data-urlencode 'redirect_uri=https://results-example.herokuapp.com/callback'

I tried the approach below but it doesn't work

this.httpClient.post(environment.typeForm.base_url + 'token', JSON.stringify({
        'grant_type' : 'authorization_code',
        'code': `${authCode}`,
        'client_id':`${*******}`,
        'client_secret':`${*******}` ,
        'redirect_uri':`${*******}`,
    }));

The grant_type shoud be 'authroization_code' as specified int the typeform documentation.

This is the error I get

{"headers":{"normalizedNames":{},"lazyUpdate":null},"status":500,"statusText":"Internal Server Error","url":"https://api.typeform.com/oauth/token","ok":false,"name":"HttpErrorResponse","message":"Http failure response for https://api.typeform.com/oauth/token: 500 Internal Server Error","error":{"code":"internal_server_error","description":"Internal server error"}}

You can find the documentation of the API I'm trying to use here

2
  • Hi can you show us what you've tried so far? Commented Jul 13, 2019 at 15:36
  • @sedders123 I've updated the question with what I've tried so far and the error i get Commented Jul 23, 2019 at 13:54

1 Answer 1

1

You can build a payload by concatenating all of your params together.

const body = `grant_type=${grant}&code=${code}&client_id=${clientId}&client_secret=${clientSecret}&redirect_uri=${redirectUrl}`;
this.httpClient.post(environment.typeForm.base_url + 'token', body);

You can also try using the FormData type to create a payload.

const payload = new FormData(); 
payload.append('grant_type', grantType); 
payload.append('code', code);
payload.append('client_id', clientId;
payload.append('client_secret', clientSecret);
payload.append('redirect_uri', redirectUrl);

this.httpClient.post(environment.typeForm.base_url + 'token', payload);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.