Skip to content

Instantly share code, notes, and snippets.

@carloswm85
Created December 10, 2021 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carloswm85/44369de183e130f3f1e99efc27d1a76b to your computer and use it in GitHub Desktop.
Save carloswm85/44369de183e130f3f1e99efc27d1a76b to your computer and use it in GitHub Desktop.

This code works correctly in Postman, but it does not work in my app, nor using this same structure in the code nor the getJson() custom function we learned to use in the class.

var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
myHeaders.append("X-MBX-APIKEY", "W4wBtjQxbUWdDiRak0kC6mbIi28zYCmghxIa6vUTUWljNilQA766nHH9RN6I1tpf");

var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  redirect: 'follow'
};

fetch("https://testnet.binance.vision/api/v3/userDataStream", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));

I've also added/removed some of the previous and following lines to the code, without success:

myHeaders.append("X-MBX-APIKEY", "W4wBtjQxbUWdDiRak0kC6mbIi28zYCmghxIa6vUTUWljNilQA766nHH9RN6I1tpf");
myHeaders.append('Access-Control-Allow-Origin', 'http://127.0.0.1:3002');
myHeaders.append('Access-Control-Allow-Methods', 'POST');
myHeaders.append('Access-Control-Allow-Headers', 'Content-Type, Authorization');
myHeaders.append('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
myHeaders.append('mode', 'no-cors');

This is the error I'm getting (one of the many I've seen).

Access to fetch at 'https://testnet.binance.vision/api/v3/userDataStream' from origin 'http://127.0.0.1:3000'
has been blocked by CORS policy: Request header field content-type is not allowed by Access-Control-Allow-Headers
in preflight response.

Possible solution(s): https://stackoverflow.com/questions/43262121/trying-to-use-fetch-and-pass-in-mode-no-cors

I'm working on some of those, especially:

As by now, Here's my project: https://github.com/carloswm85/carloswm85.github.io/blob/223969948239f21f497c09aa55a74a66e13bb5e1/portfolio/college-projects/f_2021-wdd330/projects/project02_binance-orders/src/main.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment