如何使用axios发送以下请求?

mfuanj7w  于 2023-11-18  发布在  iOS
关注(0)|答案(2)|浏览(163)

这个请求在Postman中可以正常工作,但是我试图从Axios中复制它,但是我不能


的数据
这就是我所做的(注意,我得到了相同的响应状态,但语言没有在示例本身上更新)

  1. const config = { headers: {'Content-Type': 'application/json'} };
  2. const lang ="en_US";
  3. return axios.put("https://add-jira8.infosysta.com/rest/api/2/mypreferences?key=jira.user.locale", lang,config).catch(ex => {
  4. console.log(ex)
  5. ErrorLoging.log("AxiosRequests.js >>>>> updateUserLanguage >>>>>> ERROR: " + ex)
  6. });

字符串
这是使用的API端点


slwdgvem

slwdgvem1#

你的身体有问题,甚至在你的 Postman ,你选择了json,所以它应该是一个json,像:

  1. const API_URL = "https://add-jira8.infosysta.com/rest/api/2/mypreferences?key=jira.user.locale";
  2. const config = { headers: { 'Content-Type': 'application/json' } };
  3. const body = { lang: "en_US" };
  4. return axios.put(API_URL, body, config)
  5. .catch(error => {
  6. console.log(error)
  7. });

字符串

htrmnn0y

htrmnn0y2#

查看您提供的截图,主体以“raw”发送,但在您的代码中,您试图发送JSON。不确定服务器接受哪一个,但如果它期望raw,那么您应该更改内容类型:

  1. const config = { headers: {"Content-Type": "text/plain"} };
  2. const lang ="en_US";
  3. return axios.put("https://add-jira8.infosysta.com/rest/api/2/mypreferences?key=jira.user.locale", lang,config).catch(ex => {
  4. console.log(ex)
  5. ErrorLoging.log("AxiosRequests.js >>>>> updateUserLanguage >>>>>> ERROR: " + ex)
  6. });

字符串

相关问题