将邮递员请求模拟到Axios?

时间:2018-12-07 10:00:36

标签: reactjs api react-native axios postman

enter image description here

enter image description here

我正在尝试构建自己的axios以模仿邮递员的请求,但是失败了。请帮忙看看

const ax = axios.create({
  timeout: 30000,
  headers: {
    'content-type': 'application/x-www-form-urlencoded'
  }
});

// Attempt register operation
ax.post('https://url/5bc9ff9628d79b6d274165da/update.json', {
  body: JSON.stringify({
    json: JSON.stringify({ "stat": "Delivered" })
  })
})
.then(({ data }) => {
  console.log('DEBUG::success!!! data::', data);
})
.catch((err) => {
  console.log('DEBUG::err', err);
});  

1 个答案:

答案 0 :(得分:2)

您可以使用Postman中的代码生成功能。

enter image description here 点击“代码”(位于“保存”按钮下方)>从语言下拉列表中选择“ JavaScript”> Jquery AJAX

posmtan code gen

然后,您可以仅提取所需的数据并将其粘贴到axios中。 例如。网址,标头和其他所需数据。

邮递员中用于生成代码的文档:https://learning.getpostman.com/docs/postman/sending_api_requests/generate_code_snippets/