axios 在查询参数之前没有问号打印

fzsnzjdm  于 2022-11-05  发布在  iOS
关注(0)|答案(1)|浏览(946)

我尝试的第一个方法是:

static async callSendApi(requestBody) {
    let url = new URL(

        `${API_URL}/${PAGE_ID}/messages`

    );
    url.search = new URLSearchParams({
      access_token: `${PAGE_ACCESS_TOKEN}`,
    });
    console.warn("Request body is\n" + JSON.stringify(requestBody));
    let response = await axios.post(url, {
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify(requestBody),
      // access_token: `${PAGE_ACCESS_TOKEN}`,
    });
    if (!response.ok) {
      consoleconst`Unable to call Send API: ${response.statusText}`,
        await response.json();
    }
  }

我尝试的第二种方法是:

static async callSendApi(requestBody) {
    let url = new URL(
      `${API_URL}/${PAGE_ID}/messages?access_token=${PAGE_ACCESS_TOKEN}`
    );
    /* url.search = new URLSearchParams({
      access_token: `${PAGE_ACCESS_TOKEN}`,
    });*/
    console.warn("Request body is\n" + JSON.stringify(requestBody));
    let response = await axios.post(url, {
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify(requestBody),
      // access_token: `${PAGE_ACCESS_TOKEN}`,
    });
    if (!response.ok) {
      consoleconst`Unable to call Send API: ${response.statusText}`,
        await response.json();
    }
  }

我得到错误是:

error: { 
    message: 'Unknown path components: /messagesaccess_token=access_token
    type: 'OAuthException',
    code: 2500,
    fbtrace_id: 'AbBJGVotjz3ijKKLzVE6_CM'
    }

我在两种情况下都收到了这个错误。两种情况下都跳过了'?'标记。我不知道发生了什么。我使用heroku来处理这个问题。我尝试删除并重新部署存储库来确认代码是否没有更新。但仍然出现了这个错误。:(。

ovfsdjhp

ovfsdjhp1#

我试过不使用URL和URLSearchParams,它工作!
下面是我代码:

static async callSendApi(requestBody) {

    console.warn("Request body is\n" + JSON.stringify(requestBody));

    let response = await axios.post(
      `${API_URL}/${PAGE_ID}/messages`,
      {
        params: { access_token: `${PAGE_ACCESS_TOKEN}` },
      },
      {
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify(requestBody),

      }
    );
    if (!response.ok) {
      consoleconst`Unable to call Send API: ${response.statusText}`,
        await response.json();
    }
  }

相关问题