Converting the following Paypal curl command to axios?











up vote
0
down vote

favorite












How can I convert this paypal curl command to Axios?



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"


I am using vueAxios in vue, hence the "this.".



this.axios.post('https://api.sandbox.paypal.com/v1/oauth2/token',
{'Accept': "application/json", 'Accept-Language': "en_US",
'XXXXXX': 'XXXXX', // my paypal client ID and client secret
'grant_type':'client_credentials'}
)
.then( response => {
console.log("Response is: ", response);
})
.catch( error => {
console.log("Error is :", error);
});


I am getting this error:



 Error is : Error: Request failed with status code 401
at createError (createError.js?16d0:16)
at settle (settle.js?db52:18)
at XMLHttpRequest.handleLoad (xhr.js?ec6c:77)


I've also tried this (which seems to be better but I'm still getting a 400 error):



 this.axios({ 
method: 'POST',
url: 'https://api.sandbox.paypal.com/v1/oauth2/token',
headers: {
'Accept': 'application/json',
'Accept-Language': 'en_US',
'Content-Type':'application/x-www-form-urlencoded'
},
auth: {
username: '<XXXX My paypal client ID>',
password: '<XXXX My paypal secret>'
} ,
data: {
grant_type: 'client_credentials'
},
})
.then(function(response) {console.log(response);})
.catch(function(response) {console.log(response);});


UPDATE – after some help form the comments I have tried the following code and the paypal has an issue CORS error ( i have installed a npm packages "cors" and the cors error persists (both locally and when deployed)).



This answers my question but, as stated here, it seems like Paypal doesn't allow requests directly from the browser.



this.axios({
withCredentials: true,
url: 'https://api.sandbox.paypal.com/v1/oauth2/token',
method: 'post',
headers: {
'Accept': 'application/json',
'Accept-Language': 'en_US',
'Content-Type':'application/x-www-form-urlencoded',
'Access-Control-Allow-Origin': '*',
},
data: { 'grant_type':'client_credentials' },
auth: {
username: 'XXXXXXXX',
password: 'XXXXXXXX'
}
})


Related documentation:



CORS: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS



VueAxios: https://www.npmjs.com/package/vue-axios



Paypal dev: https://developer.paypal.com/docs/api/overview/#make-your-first-call










share|improve this question




















  • 2




    No clue about axios, but I'm pretty sure that you mixed headers and the payload.
    – maio290
    Nov 19 at 16:24










  • Yes. I don't know how to inform axios about what --header is, the --User credentials are and what's the --data... :/
    – Joao Alves Marrucho
    Nov 19 at 16:27















up vote
0
down vote

favorite












How can I convert this paypal curl command to Axios?



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"


I am using vueAxios in vue, hence the "this.".



this.axios.post('https://api.sandbox.paypal.com/v1/oauth2/token',
{'Accept': "application/json", 'Accept-Language': "en_US",
'XXXXXX': 'XXXXX', // my paypal client ID and client secret
'grant_type':'client_credentials'}
)
.then( response => {
console.log("Response is: ", response);
})
.catch( error => {
console.log("Error is :", error);
});


I am getting this error:



 Error is : Error: Request failed with status code 401
at createError (createError.js?16d0:16)
at settle (settle.js?db52:18)
at XMLHttpRequest.handleLoad (xhr.js?ec6c:77)


I've also tried this (which seems to be better but I'm still getting a 400 error):



 this.axios({ 
method: 'POST',
url: 'https://api.sandbox.paypal.com/v1/oauth2/token',
headers: {
'Accept': 'application/json',
'Accept-Language': 'en_US',
'Content-Type':'application/x-www-form-urlencoded'
},
auth: {
username: '<XXXX My paypal client ID>',
password: '<XXXX My paypal secret>'
} ,
data: {
grant_type: 'client_credentials'
},
})
.then(function(response) {console.log(response);})
.catch(function(response) {console.log(response);});


UPDATE – after some help form the comments I have tried the following code and the paypal has an issue CORS error ( i have installed a npm packages "cors" and the cors error persists (both locally and when deployed)).



This answers my question but, as stated here, it seems like Paypal doesn't allow requests directly from the browser.



this.axios({
withCredentials: true,
url: 'https://api.sandbox.paypal.com/v1/oauth2/token',
method: 'post',
headers: {
'Accept': 'application/json',
'Accept-Language': 'en_US',
'Content-Type':'application/x-www-form-urlencoded',
'Access-Control-Allow-Origin': '*',
},
data: { 'grant_type':'client_credentials' },
auth: {
username: 'XXXXXXXX',
password: 'XXXXXXXX'
}
})


Related documentation:



CORS: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS



VueAxios: https://www.npmjs.com/package/vue-axios



Paypal dev: https://developer.paypal.com/docs/api/overview/#make-your-first-call










share|improve this question




















  • 2




    No clue about axios, but I'm pretty sure that you mixed headers and the payload.
    – maio290
    Nov 19 at 16:24










  • Yes. I don't know how to inform axios about what --header is, the --User credentials are and what's the --data... :/
    – Joao Alves Marrucho
    Nov 19 at 16:27













up vote
0
down vote

favorite









up vote
0
down vote

favorite











How can I convert this paypal curl command to Axios?



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"


I am using vueAxios in vue, hence the "this.".



this.axios.post('https://api.sandbox.paypal.com/v1/oauth2/token',
{'Accept': "application/json", 'Accept-Language': "en_US",
'XXXXXX': 'XXXXX', // my paypal client ID and client secret
'grant_type':'client_credentials'}
)
.then( response => {
console.log("Response is: ", response);
})
.catch( error => {
console.log("Error is :", error);
});


I am getting this error:



 Error is : Error: Request failed with status code 401
at createError (createError.js?16d0:16)
at settle (settle.js?db52:18)
at XMLHttpRequest.handleLoad (xhr.js?ec6c:77)


I've also tried this (which seems to be better but I'm still getting a 400 error):



 this.axios({ 
method: 'POST',
url: 'https://api.sandbox.paypal.com/v1/oauth2/token',
headers: {
'Accept': 'application/json',
'Accept-Language': 'en_US',
'Content-Type':'application/x-www-form-urlencoded'
},
auth: {
username: '<XXXX My paypal client ID>',
password: '<XXXX My paypal secret>'
} ,
data: {
grant_type: 'client_credentials'
},
})
.then(function(response) {console.log(response);})
.catch(function(response) {console.log(response);});


UPDATE – after some help form the comments I have tried the following code and the paypal has an issue CORS error ( i have installed a npm packages "cors" and the cors error persists (both locally and when deployed)).



This answers my question but, as stated here, it seems like Paypal doesn't allow requests directly from the browser.



this.axios({
withCredentials: true,
url: 'https://api.sandbox.paypal.com/v1/oauth2/token',
method: 'post',
headers: {
'Accept': 'application/json',
'Accept-Language': 'en_US',
'Content-Type':'application/x-www-form-urlencoded',
'Access-Control-Allow-Origin': '*',
},
data: { 'grant_type':'client_credentials' },
auth: {
username: 'XXXXXXXX',
password: 'XXXXXXXX'
}
})


Related documentation:



CORS: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS



VueAxios: https://www.npmjs.com/package/vue-axios



Paypal dev: https://developer.paypal.com/docs/api/overview/#make-your-first-call










share|improve this question















How can I convert this paypal curl command to Axios?



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"


I am using vueAxios in vue, hence the "this.".



this.axios.post('https://api.sandbox.paypal.com/v1/oauth2/token',
{'Accept': "application/json", 'Accept-Language': "en_US",
'XXXXXX': 'XXXXX', // my paypal client ID and client secret
'grant_type':'client_credentials'}
)
.then( response => {
console.log("Response is: ", response);
})
.catch( error => {
console.log("Error is :", error);
});


I am getting this error:



 Error is : Error: Request failed with status code 401
at createError (createError.js?16d0:16)
at settle (settle.js?db52:18)
at XMLHttpRequest.handleLoad (xhr.js?ec6c:77)


I've also tried this (which seems to be better but I'm still getting a 400 error):



 this.axios({ 
method: 'POST',
url: 'https://api.sandbox.paypal.com/v1/oauth2/token',
headers: {
'Accept': 'application/json',
'Accept-Language': 'en_US',
'Content-Type':'application/x-www-form-urlencoded'
},
auth: {
username: '<XXXX My paypal client ID>',
password: '<XXXX My paypal secret>'
} ,
data: {
grant_type: 'client_credentials'
},
})
.then(function(response) {console.log(response);})
.catch(function(response) {console.log(response);});


UPDATE – after some help form the comments I have tried the following code and the paypal has an issue CORS error ( i have installed a npm packages "cors" and the cors error persists (both locally and when deployed)).



This answers my question but, as stated here, it seems like Paypal doesn't allow requests directly from the browser.



this.axios({
withCredentials: true,
url: 'https://api.sandbox.paypal.com/v1/oauth2/token',
method: 'post',
headers: {
'Accept': 'application/json',
'Accept-Language': 'en_US',
'Content-Type':'application/x-www-form-urlencoded',
'Access-Control-Allow-Origin': '*',
},
data: { 'grant_type':'client_credentials' },
auth: {
username: 'XXXXXXXX',
password: 'XXXXXXXX'
}
})


Related documentation:



CORS: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS



VueAxios: https://www.npmjs.com/package/vue-axios



Paypal dev: https://developer.paypal.com/docs/api/overview/#make-your-first-call







curl vue.js paypal httprequest axios






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 19:24

























asked Nov 19 at 16:21









Joao Alves Marrucho

337213




337213








  • 2




    No clue about axios, but I'm pretty sure that you mixed headers and the payload.
    – maio290
    Nov 19 at 16:24










  • Yes. I don't know how to inform axios about what --header is, the --User credentials are and what's the --data... :/
    – Joao Alves Marrucho
    Nov 19 at 16:27














  • 2




    No clue about axios, but I'm pretty sure that you mixed headers and the payload.
    – maio290
    Nov 19 at 16:24










  • Yes. I don't know how to inform axios about what --header is, the --User credentials are and what's the --data... :/
    – Joao Alves Marrucho
    Nov 19 at 16:27








2




2




No clue about axios, but I'm pretty sure that you mixed headers and the payload.
– maio290
Nov 19 at 16:24




No clue about axios, but I'm pretty sure that you mixed headers and the payload.
– maio290
Nov 19 at 16:24












Yes. I don't know how to inform axios about what --header is, the --User credentials are and what's the --data... :/
– Joao Alves Marrucho
Nov 19 at 16:27




Yes. I don't know how to inform axios about what --header is, the --User credentials are and what's the --data... :/
– Joao Alves Marrucho
Nov 19 at 16:27












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










According to the axios GitHub docs:



this.axios({
url: 'https://api.sandbox.paypal.com/v1/oauth2/token',
method: 'post',
headers: {
'Accept': 'application/json',
'Accept-Language': 'en_US',
'Content-Type':'application/x-www-form-urlencoded',
'Access-Control-Allow-Origin': '*',
},
data: { 'grant_type':'client_credentials' },
auth: {
username: client_id,
password: secret
}
})





share|improve this answer























  • thanks but that doesn't work I get this 415 error: POST api.sandbox.paypal.com/v1/oauth2/token 415 (Unsupported Media Type)... so I have added the content type Header to try and fix it: 'Content-Type':'application/x-www-form-urlencoded' and it now returns a 400 error
    – Joao Alves Marrucho
    Nov 19 at 16:59












  • When you do the curl command, does it work?
    – Psidom
    Nov 19 at 17:00






  • 1




    Hmm. Could you try add withCredentials: true to this.axios({})?
    – Psidom
    Nov 19 at 17:08






  • 1




    ok. Not sure if your server allows cross origin request, but try add { 'Access-Control-Allow-Origin': 'Authorization' } to the headers.
    – Psidom
    Nov 19 at 18:07








  • 1




    Also I am not sure PayPal API allows you to directly make request from the browser. If not, then you need to set up a server to make request on behalf of your frontend; that is, set up a server to make these requests and then send result to your frontend;
    – Psidom
    Nov 19 at 18:14











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378792%2fconverting-the-following-paypal-curl-command-to-axios%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










According to the axios GitHub docs:



this.axios({
url: 'https://api.sandbox.paypal.com/v1/oauth2/token',
method: 'post',
headers: {
'Accept': 'application/json',
'Accept-Language': 'en_US',
'Content-Type':'application/x-www-form-urlencoded',
'Access-Control-Allow-Origin': '*',
},
data: { 'grant_type':'client_credentials' },
auth: {
username: client_id,
password: secret
}
})





share|improve this answer























  • thanks but that doesn't work I get this 415 error: POST api.sandbox.paypal.com/v1/oauth2/token 415 (Unsupported Media Type)... so I have added the content type Header to try and fix it: 'Content-Type':'application/x-www-form-urlencoded' and it now returns a 400 error
    – Joao Alves Marrucho
    Nov 19 at 16:59












  • When you do the curl command, does it work?
    – Psidom
    Nov 19 at 17:00






  • 1




    Hmm. Could you try add withCredentials: true to this.axios({})?
    – Psidom
    Nov 19 at 17:08






  • 1




    ok. Not sure if your server allows cross origin request, but try add { 'Access-Control-Allow-Origin': 'Authorization' } to the headers.
    – Psidom
    Nov 19 at 18:07








  • 1




    Also I am not sure PayPal API allows you to directly make request from the browser. If not, then you need to set up a server to make request on behalf of your frontend; that is, set up a server to make these requests and then send result to your frontend;
    – Psidom
    Nov 19 at 18:14















up vote
2
down vote



accepted










According to the axios GitHub docs:



this.axios({
url: 'https://api.sandbox.paypal.com/v1/oauth2/token',
method: 'post',
headers: {
'Accept': 'application/json',
'Accept-Language': 'en_US',
'Content-Type':'application/x-www-form-urlencoded',
'Access-Control-Allow-Origin': '*',
},
data: { 'grant_type':'client_credentials' },
auth: {
username: client_id,
password: secret
}
})





share|improve this answer























  • thanks but that doesn't work I get this 415 error: POST api.sandbox.paypal.com/v1/oauth2/token 415 (Unsupported Media Type)... so I have added the content type Header to try and fix it: 'Content-Type':'application/x-www-form-urlencoded' and it now returns a 400 error
    – Joao Alves Marrucho
    Nov 19 at 16:59












  • When you do the curl command, does it work?
    – Psidom
    Nov 19 at 17:00






  • 1




    Hmm. Could you try add withCredentials: true to this.axios({})?
    – Psidom
    Nov 19 at 17:08






  • 1




    ok. Not sure if your server allows cross origin request, but try add { 'Access-Control-Allow-Origin': 'Authorization' } to the headers.
    – Psidom
    Nov 19 at 18:07








  • 1




    Also I am not sure PayPal API allows you to directly make request from the browser. If not, then you need to set up a server to make request on behalf of your frontend; that is, set up a server to make these requests and then send result to your frontend;
    – Psidom
    Nov 19 at 18:14













up vote
2
down vote



accepted







up vote
2
down vote



accepted






According to the axios GitHub docs:



this.axios({
url: 'https://api.sandbox.paypal.com/v1/oauth2/token',
method: 'post',
headers: {
'Accept': 'application/json',
'Accept-Language': 'en_US',
'Content-Type':'application/x-www-form-urlencoded',
'Access-Control-Allow-Origin': '*',
},
data: { 'grant_type':'client_credentials' },
auth: {
username: client_id,
password: secret
}
})





share|improve this answer














According to the axios GitHub docs:



this.axios({
url: 'https://api.sandbox.paypal.com/v1/oauth2/token',
method: 'post',
headers: {
'Accept': 'application/json',
'Accept-Language': 'en_US',
'Content-Type':'application/x-www-form-urlencoded',
'Access-Control-Allow-Origin': '*',
},
data: { 'grant_type':'client_credentials' },
auth: {
username: client_id,
password: secret
}
})






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 at 19:24

























answered Nov 19 at 16:51









Psidom

121k1180124




121k1180124












  • thanks but that doesn't work I get this 415 error: POST api.sandbox.paypal.com/v1/oauth2/token 415 (Unsupported Media Type)... so I have added the content type Header to try and fix it: 'Content-Type':'application/x-www-form-urlencoded' and it now returns a 400 error
    – Joao Alves Marrucho
    Nov 19 at 16:59












  • When you do the curl command, does it work?
    – Psidom
    Nov 19 at 17:00






  • 1




    Hmm. Could you try add withCredentials: true to this.axios({})?
    – Psidom
    Nov 19 at 17:08






  • 1




    ok. Not sure if your server allows cross origin request, but try add { 'Access-Control-Allow-Origin': 'Authorization' } to the headers.
    – Psidom
    Nov 19 at 18:07








  • 1




    Also I am not sure PayPal API allows you to directly make request from the browser. If not, then you need to set up a server to make request on behalf of your frontend; that is, set up a server to make these requests and then send result to your frontend;
    – Psidom
    Nov 19 at 18:14


















  • thanks but that doesn't work I get this 415 error: POST api.sandbox.paypal.com/v1/oauth2/token 415 (Unsupported Media Type)... so I have added the content type Header to try and fix it: 'Content-Type':'application/x-www-form-urlencoded' and it now returns a 400 error
    – Joao Alves Marrucho
    Nov 19 at 16:59












  • When you do the curl command, does it work?
    – Psidom
    Nov 19 at 17:00






  • 1




    Hmm. Could you try add withCredentials: true to this.axios({})?
    – Psidom
    Nov 19 at 17:08






  • 1




    ok. Not sure if your server allows cross origin request, but try add { 'Access-Control-Allow-Origin': 'Authorization' } to the headers.
    – Psidom
    Nov 19 at 18:07








  • 1




    Also I am not sure PayPal API allows you to directly make request from the browser. If not, then you need to set up a server to make request on behalf of your frontend; that is, set up a server to make these requests and then send result to your frontend;
    – Psidom
    Nov 19 at 18:14
















thanks but that doesn't work I get this 415 error: POST api.sandbox.paypal.com/v1/oauth2/token 415 (Unsupported Media Type)... so I have added the content type Header to try and fix it: 'Content-Type':'application/x-www-form-urlencoded' and it now returns a 400 error
– Joao Alves Marrucho
Nov 19 at 16:59






thanks but that doesn't work I get this 415 error: POST api.sandbox.paypal.com/v1/oauth2/token 415 (Unsupported Media Type)... so I have added the content type Header to try and fix it: 'Content-Type':'application/x-www-form-urlencoded' and it now returns a 400 error
– Joao Alves Marrucho
Nov 19 at 16:59














When you do the curl command, does it work?
– Psidom
Nov 19 at 17:00




When you do the curl command, does it work?
– Psidom
Nov 19 at 17:00




1




1




Hmm. Could you try add withCredentials: true to this.axios({})?
– Psidom
Nov 19 at 17:08




Hmm. Could you try add withCredentials: true to this.axios({})?
– Psidom
Nov 19 at 17:08




1




1




ok. Not sure if your server allows cross origin request, but try add { 'Access-Control-Allow-Origin': 'Authorization' } to the headers.
– Psidom
Nov 19 at 18:07






ok. Not sure if your server allows cross origin request, but try add { 'Access-Control-Allow-Origin': 'Authorization' } to the headers.
– Psidom
Nov 19 at 18:07






1




1




Also I am not sure PayPal API allows you to directly make request from the browser. If not, then you need to set up a server to make request on behalf of your frontend; that is, set up a server to make these requests and then send result to your frontend;
– Psidom
Nov 19 at 18:14




Also I am not sure PayPal API allows you to directly make request from the browser. If not, then you need to set up a server to make request on behalf of your frontend; that is, set up a server to make these requests and then send result to your frontend;
– Psidom
Nov 19 at 18:14


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378792%2fconverting-the-following-paypal-curl-command-to-axios%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

If I really need a card on my start hand, how many mulligans make sense? [duplicate]

Alcedinidae

Can an atomic nucleus contain both particles and antiparticles? [duplicate]