首页 > 解决方案 > 帖子正文中缺少必需的参数 To

问题描述

我不确定标题或正文是否配置不正确。关于是否更改标题或正文配置错误的任何想法?

const axios = require('axios');
  const url = '/my_url';
  const auth = {
    username: username,
    password: password
  };

  const requestbody = {
    To: 'phone',
    From: 'phone 2'
  };

  const headers = {
    'Content-Type': 'application/x-www-form-urlencoded',
  }
  const config = {
    auth: auth,
    headers: headers
  }

  try {
    const response = await axios.post(url, {data: requestbody}, config);
    console.log(response);
  } catch (error) {
    console.error(error);
  }

错误如: message: 'Missing required parameter To in the post body'

标签: javascriptnode.jsaxios

解决方案


您需要将参数字符串化,然后直接传递

const querystring = require('query-string');

  const query = querystring.stringify({
  To: 'phone',
    From: 'phone 2'
  });
  let options = {
    headers: {
      'Authorization': AUTH_HEADER,
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  };
    let axios_res = await post(url, query , options);

推荐阅读