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

328
Views
¿Cómo puedo convertir esta llamada curl a una llamada XML Http Request?

Tengo algo como esto :

 curl "https://test.api.amadeus.com/v1/security/oauth2/token" \ -H "Content-Type: application/x-www-form-urlencoded" \ -d "grant_type=client_credentials&client_id={client_id}&client_secret={client_secret}"

Quiero convertirlo en objeto xhr en Javascript. Me puedes ayudar ? Quiero decir ;

 xhr.open(....) xhr.setRequestHeader(.....) xhr.send(.....)
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Te recomiendo que no lo hagas. :-) En su lugar, use fetch , el reemplazo estándar más nuevo para XMLHttpRequest . Solo tenga cuidado con la pistola en la API que describo aquí . Para ver cómo, veamos qué está haciendo esa llamada curl :

  • -H define un encabezado.

  • -d especifica los datos que se curl ...

    ...envía...en una solicitud POST al servidor HTTP, de la misma manera que lo hace un navegador cuando un usuario ha llenado un formulario HTML y presiona el botón de enviar. Esto hará que curl pase los datos al servidor utilizando el tipo de contenido application/x-www-form-urlencoded.

Entonces, mirando las formas de fetch que haces eso:

  • Los encabezados se definen mediante el objeto de headers en el inicializador de solicitudes.
  • Los datos se envían a través de la propiedad del body del inicializador de la solicitud.
  • Especifique una solicitud POST a través de la propiedad del method en el inicializador de solicitud.
  • Para enviar datos compatibles con application/x-www-form-urlencoded , lo más fácil es utilizar un objeto URLSearchParams .

Entonces eso nos da:

 fetch("https://test.api.amadeus.com/v1/security/oauth2/token", { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded" }, body: new URLSearchParams([ ["grant_type", "client_credentials"], ["client_id", client_id], // I'm assuming you have this in a variable ["client_secret", client_secret], // Same assumption ]), }) .then(response => { if (!response.ok) { throw new Error(`HTTP error ${response.status}`); } // ...if relevant, read the response via `json` or `text` or others; // here I'll use `text` AS AN EXAMPLE return response.text(); }) .then(data => { // ...use the data... }) .catch(error => { // ...handle/report error... });
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!