You just add HTTP headers as an additional object after ‘method’.
Below shows how to use Content-Type header.
Note that there is a dash in “Content-Type”. Also all header content may be case and punctuation sensitive, so be super careful with what you are putting into the headers, or it can get ignored or even cause an error.
import {fetch} from 'wix-fetch';
$w.onReady(function () {
myFetch().then((result) => {
console.log(result);
});
});
function myFetch(){
let url = "https://api.fixer.io/latest";
return fetch(url, {
method: 'get',
headers: {
'Content-Type':'application/json'
}
})
.then((httpResponse) => {
if(httpResponse.ok){
return httpResponse.json();
}
else {
return Promise.reject("Fetch failed");
}
})
.then((json) => {
let s = JSON.stringify(json);
return s;
})
.catch( (err) => {
console.log("Error: ");
console.log(err);
});
}
‘user’ and ‘pass’ are not HTTP headers, so you probably want to try to add them to the URL as GET parameters. I would presume that the same applies to form fields. E.g.:
https://example.com/page/?user=whoami&pass=helloworld
However, it may be that you require some other form of authentication which in fact could be passed via headers but in a more long-winded way. Try to check how authentication works there first.