0

I need to consume a HTTP endpoint using a specific media type and that endpoint doesn't handle the OPTIONS method.

I imagine this would be impossible using regular browsers but I belive it should be possible using Electron, since I can do the same POST request using Postman.

What kind of framework do I have to use to achieve that?

pca1987
  • 323
  • 3
  • 15
  • I didn't downvote. But it's not clear at all what you're asking. What does that endpoint expect? Obviously not CORS because then it would understand OPTIONS... using JavaScript you can generate pretty much any HTTP request... nothing to do with electron and you don't need postman. – mb21 Sep 07 '18 at 12:28
  • I mentioned postman because it was developed in Electron, so I know it is possible to do what I want in Electron. The endpoint expects a custom mediatype, if I do the POST request using angular or any javascript framework using a regular browser, it sends a preflight OPTIONS method that the endpoint does not support – pca1987 Sep 07 '18 at 12:43

1 Answers1

2

I got it working. If you use Angular, jQuery or any Javascript inside Electron, it will use the Browser's capabilities and therefore will also send the OPTIONS preflight if the POST has a complex media type, which was my case.

If you use Electron's http API, it does not do that. Documentation is here https://electronjs.org/docs/api/client-request

Here is my POC angular code using it:

  const { net } = require('electron').remote;
  const request = net.request(requestApi);

let requestApi = {
    method: 'POST',
    headers: {
      'Content-Type': 'custom complex media type here',
      'Authorization': 'Bearer ' + accessToken // if api is secured
    },
    protocol: 'https:',
    hostname: 'hostname.com',
    port: 443,
    path: '/api/path/to/method'
  };

  request.on('response', (response) => {
    console.log(`STATUS: ${response.statusCode}`);
    resolve(response);

    response.on('error', (error) => {
      console.log(`ERROR: ${JSON.stringify(error)}`);
      reject(error);
    })
  });

  request.end(JSON.stringify(usageData));

Hope this helps.

pca1987
  • 323
  • 3
  • 15