Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

106
Views
cómo traducir curl en una solicitud POST de javascript

Estoy tratando de conectarme a la API de pago llamada PayU, brindan un ejemplo de cómo conectarse a través de curl uno es conectarse a través de la API de descanso

 curl -X POST https://secure.payu.com/pl/standard/user/oauth/authorize \ -d 'grant_type=client_credentials&client_id=145227&client_secret=12f071174cb7eb79d4aac5bc2f07563f'

y uno es para conectarme a través de SDK, que también me gustaría usar, pero este necesita configuraciones adicionales en la tienda, y tengo problemas con el primero, así que si alguien tuviera la amabilidad de descifrar el otro también lo haría. Se bueno

 curl -X POST https://secure.payu.com/pl/standard/user/oauth/authorize \ -H "Cache-Control: no-cache" -H "Content-Type: application/x-www-form-urlencoded" -d 'grant_type=trusted_merchant&client_id=[provided by PayU]&client_secret=[provided by PayU]&email=[users email]&ext_customer_id=[Id of the customer used in merchant system]'

En curl, el primero entregó el token sin problemas, sin embargo, estoy tratando de hacer lo mismo en el código y no puedo. Este es mi código:

 fetch('https://secure.payu.com/pl/standard/user/oauth/authorize', { method: 'POST', body: JSON.stringify({ 'grant_type': 'client_credentials', 'client_id': '145227', 'client_secret': '12f071174cb7eb79d4aac5bc2f07563f', }) }).then(res => { if (!res.ok) { throw new Error("Fetching payU failed, please try again later!"); } return res; }) .then(data => { console.log(data) return { payUdata: data } }) .catch(err => { console.log(err); });
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Un cuerpo de publicación básico no es un json serializado, los datos se ven como parámetros de consulta, al igual que en el curl

 { method: 'POST', headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'}, body: "grant_type=client_credentials&client_id=145227&client_secret=12f071174cb7eb79d4aac5bc2f07563f") }

del comentario de @BankBuilder:

 function querifyObject(obj){ return new URLSearchParams(Object.entries(obj)).toString(); }

y entonces:

 { method: 'POST', headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'}, body: querifyObject({ 'grant_type': 'client_credentials', 'client_id': '145227', 'client_secret': '12f071174cb7eb79d4aac5bc2f07563f', }) }

Captura de pantalla de trabajo

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!