How can I make a POST request in a VSCode extension

好久不见. 提交于 2021-02-10 05:26:15

问题


I want to make a VSC extension that involves posting to my API, however when I write my fetch syntax out to POST to my server, it doesn't work. So I thought maybe I need to add node-fetch, so I did npm i --save node-fetch and it says This expression is not callable. and once again, it still can't make the POST request.


回答1:


I have used axios to post to a URL:

import * as FormData from 'form-data';
import axios from 'axios';

          const form = new FormData();
          form.append('srcmbr', save_folderContent.srcmbr);
          form.append('srcfName', save_folderContent.srcfName);
          form.append('srcfLib', save_folderContent.srcfLib);

          const headers = form.getHeaders();
          headers['Content-length'] = await form_getLength(form);
          {
            const result = await axios.post(
              `${serverUrl}/site/common/rmvm-srcmbr.php`, form,
              { headers, });
            console.log(`delete-srcmbr ${result.data}`);
          }
export function form_getLength(form: FormData)
{
  return new Promise((resolve, reject) =>
  {
    form.getLength((err, length) =>
    {
      resolve(length);
    });
  });
}



回答2:


I forgot to add .default at the end of the axios require.

so it would be

const axios = require('axios').default;

IF YOU'RE USING TYPESCRIPT PLEASE REFER TO @RockBoro 's POST!!!



来源:https://stackoverflow.com/questions/65146333/how-can-i-make-a-post-request-in-a-vscode-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!