Currently, I am utilizing the Fetch API in react-native along with typescript. The code snippet I am working with looks like this:
let responseLogin = await fetch('http://url_example', {
method: 'POST',
headers: {'Content-Type':'application/json'},
body: requestBody
});
However, upon running the code, I encounter an error related to the header section:
Argument of type '{ method: string; headers: { 'Content-Type': string; }; body: string; }' is not assignable to parameter of type 'RequestInit'.
Types of property 'headers' are incompatible.
Type '{ 'Content-Type': string; }' is not assignable to type 'Headers | string[][]'.
Object literal may only specify known properties, and ''Content-Type'' does not exist in type 'Headers | string[][]'.
I have attempted creating a custom header as well but unfortunately, it did not yield any positive results:
let requestHeaders = new Headers();
requestHeaders.set('Content-Type', 'application/json');
// Additional attempt that was made but didn't resolve the issue
// requestHeaders.get('Content-Type');
Is there a way to successfully add a header in this scenario? I've tried various methods without success and cannot pinpoint the exact cause of the problem. While testing in postman yields a 200 response, executing the code produces a 401 response. I also experimented by using the library 'fetch-headers' to include custom headers: https://www.npmjs.com/package/fetch-headers
Tools being used: Visual studio code version 1.81.1 "react-native": "0.50.0", "typescript": "2.6.1"