如何在 ReactJS 中使用curl命令

joh*_*bab 4 api reactjs

我需要在我的网站上使用一个 API,该 API 的文档是用 CURL 命令编写的。具体来说,我想知道如何在我的 React 应用程序上使用这样的命令。

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"
Run Code Online (Sandbox Code Playgroud)

小智 5

您可以重写它来获取请求

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"
})
Run Code Online (Sandbox Code Playgroud)