How to use curl commands with ReactJS

Viewed 5797

I need to use an API in my website that has it documentation written in CURL commands. Specifically I would like to know how can I use a command like this on my React app.

curl -H token:"sfg999666t673t7t82" -H "Content-Type: application/json" -H "accept: application/json" -d '{"id":"3970a1b0-6e27-448a-adfc-0083db15b2fb", "tokens":{"design_token1":"Hi","design_token2":"Hello","design_token3":"World","subject_token1":"XYZ"}, "recipient":"james@sample.com"}' -X POST "https://domain.provider.com/mas/api/v1/mail/transaction"

2 Answers

You can rewrite it to fetch request

fetch("https://domain.provider.com/mas/api/v1/mail/transaction", {
  body: "{\"id\":\"3970a1b0-6e27-448a-adfc-0083db15b2fb\", \"tokens\":{\"design_token1\":\"Hi\",\"design_token2\":\"Hello\",\"design_token3\":\"World\",\"subject_token1\":\"XYZ\"}, \"recipient\":\"james@sample.com\"}",
  headers: {
    Accept: "application/json",
    "Content-Type": "application/json",
    Token: "sfg999666t673t7t82"
  },
  method: "POST"
})

I would recommend to use js library like axios to perform request. Curl is just one of many options.. If you are looking for less programming way to do this you can use http clients like insomnia or postman and autogenerate code for your needs.

Related