Home > Software engineering >  What is the javascript/React equivalent code for this curl code - Capital.com API (React.js)
What is the javascript/React equivalent code for this curl code - Capital.com API (React.js)

Time:01-02

I have a curl code I took from the capital.com website regarding their API but they do not have docs for node.js and I have no clue what curl code is so I do not understand it, Can anyone tell me the js equivalent assuming I am using axios to make the request.

curl -L -X POST 'https://api-capital.backend-capital.com/api/v1/session' \
-H 'X-CAP-API-KEY: evSl********S26P' \
-H 'Content-Type: application/json' \
--data-raw '{
 "encryptedPassword": "false", 
 "identifier": "[email protected]",
 "password": "Xxxx9999_"
}'

I tried this below but gave me a error Status 400

const loginHandler = () => {
    axios.post("https://api-capital.backend-capital.com/api/v1/session", {
      'X-CAP-API-KEY': "tL0xi4wvz5X8uHaD",
      'Content-Type': 'application/json',
      'data-raw': {
        'encryptedPassword': false,
        "identifier": "hadwe",
        'password': '88asc'
      }
    }).then(res => {
      console.log(res.data)
    })
  }

CodePudding user response:

--data-raw is the body.

const response = await axios.post(
  'https://api-capital.backend-capital.com/api/v1/session',
  {
    'encryptedPassword': 'false',
    'identifier': '[email protected]',
    'password': 'Xxxx9999_'
  },
  {
    headers: {
      'X-CAP-API-KEY': 'evSl********S26P',
      'Content-Type': 'application/json'
    }
  }
)

CodePudding user response:

Zac's answer above is perfect, the body data which is curl is the --raw-data is the 2nd parameter of the axios.post function, and the configuration for the request is the 3rd, in this configuration you can set the headers.

I recommend using Typescript when trying to figure out these libraries, because with Intellisense or even just TS language server you get a tone of help, basically telling you what each method's interface is right on your editor.

  • Related