Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot fetch stream api response #48314

Open
huseyinaltas opened this issue Dec 17, 2024 · 5 comments
Open

Cannot fetch stream api response #48314

huseyinaltas opened this issue Dec 17, 2024 · 5 comments
Labels
Needs: Attention Issues where the author has responded to feedback. 🌐Networking Related to a networking API. Type: Unsupported Version Issues reported to a version of React Native that is no longer supported

Comments

@huseyinaltas
Copy link

Description

When I implement this fetch curl api on node.js standalone project, I get stream response,
And I implement in react-native project I cannot get response

async function chat(prompt) {

const API_KEY = "XXXX";
const url = new URL(
https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-exp:streamGenerateContent?key={API_KEY}
);
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
contents: [
{
parts: [{ text: prompt }],
},
],
}),
});

if (!response.ok) {
console.error("Request failed", response.statusText);
return;
}

const reader = response.body.getReader();
const decoder = new TextDecoder();
let done = false;
let content = "";

while (!done) {
const { value, done: readerDone } = await reader.read();
done = readerDone;
content += decoder.decode(value, { stream: true });
console.log(content);
}

// Final content received
console.log("Final Content:", content);
}

Steps to reproduce

Copy above code and use fetch in single node.js project
It gets logging out stream response

When I copy and past on react-native project
And I call it,
Not getting response.body in fetch

"dependencies": {
"@google/generative-ai": "^0.21.0",
"@react-native-firebase/analytics": "^19.3.0",
"@react-native-firebase/app": "^19.3.0",
"@react-native-firebase/auth": "^19.3.0",
"@react-native-firebase/firestore": "19.3.0",
"@react-native-firebase/functions": "^19.3.0",
"@react-native-firebase/messaging": "^19.3.0",
"@react-native-firebase/remote-config": "^19.3.0",
"@react-navigation/stack": "^6.3.29",
"axios": "^1.7.9",
"country-flag-icons": "^1.5.13",
"https-browserify": "^1.0.0",
"moment-timezone": "^0.5.45",
"react": "18.2.0",
"react-native": "^0.73.9",
"react-native-device-info": "^10.13.1",
"react-native-fs": "^2.20.0",
"react-native-gesture-handler": "^2.16.1",
"react-native-keychain": "^8.1.3",
"react-native-localize": "^3.2.1",
"react-native-paper": "^5.12.3",
"react-native-safe-area-context": "^4.9.0",
"react-native-screens": "^3.29.0",
"react-native-sound": "^0.11.2",
"react-native-sse": "^1.2.1",
"react-native-vector-icons": "^10.0.3"
},
"devDependencies": {
"@babel/core": "^7.20.0",
"@babel/preset-env": "^7.20.0",
"@babel/runtime": "^7.20.0",
"@react-native/babel-preset": "0.73.21",
"@react-native/eslint-config": "0.73.2",
"@react-native/metro-config": "0.73.5",
"@react-native/typescript-config": "0.73.1",
"@types/react": "^18.2.6",
"@types/react-native-vector-icons": "^6.4.18",
"@types/react-test-renderer": "^18.0.0",
"babel-jest": "^29.6.3",
"eslint": "^8.19.0",
"jest": "^29.6.3",
"prettier": "2.8.8",
"react-test-renderer": "18.2.0",
"typescript": "5.0.4"
},

React Native Version

"react-native": "^0.73.9"

Affected Platforms

Runtime - Android, Runtime - iOS

Output of npx react-native info

OS: macOS 15.2 
  CPU: (12) arm64 Apple M2 Pro
  Memory: 202.84 MB / 16.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 23.3.0
    path: /opt/homebrew/bin/node
  Yarn: Not Found
  npm:
    version: 10.9.0
    path: /opt/homebrew/bin/npm
  Watchman:
    version: 2024.11.18.00
    path: /opt/homebrew/bin/watchman
Managers:
  CocoaPods:
    version: 1.16.2
    path: /usr/local/bin/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 24.1
      - iOS 18.1
      - macOS 15.1
      - tvOS 18.1
      - visionOS 2.1
      - watchOS 11.1
  Android SDK: Not Found
IDEs:
  Android Studio: 2023.3 AI-233.14808.21.2331.11709847
  Xcode:
    version: 16.1/16B40
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 17.0.11
    path: /usr/bin/javac
  Ruby:
    version: 2.6.10
    path: /usr/bin/ruby
npmPackages:
  "@react-native-community/cli": Not Found
  react:
    installed: 18.2.0
    wanted: 18.2.0
  react-native:
    installed: 0.73.9
    wanted: ^0.73.9
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: false
iOS:
  hermesEnabled: true
  newArchEnabled: false

Stacktrace or Logs

ERROR  Error fetching data: [TypeError: Cannot read property 'read' of undefined]

Reproducer

https://github.com/huseyinaltas/Shock

Screenshots and Videos

No response

@react-native-bot
Copy link
Collaborator

Warning

Unsupported version: It looks like your issue or the example you provided uses an unsupported version of React Native.

Due to the number of issues we receive, we're currently only accepting new issues against one of the supported versions. Please upgrade to latest and verify if the issue persists (alternatively, create a new project and repro the issue in it). If you cannot upgrade, please open your issue on StackOverflow to get further community support.

@react-native-bot react-native-bot added Type: Unsupported Version Issues reported to a version of React Native that is no longer supported Needs: Author Feedback 🌐Networking Related to a networking API. and removed Needs: Triage 🔍 labels Dec 17, 2024
@react-native-bot
Copy link
Collaborator

Warning

Unsupported version: It looks like your issue or the example you provided uses an unsupported version of React Native.

Due to the number of issues we receive, we're currently only accepting new issues against one of the supported versions. Please upgrade to latest and verify if the issue persists (alternatively, create a new project and repro the issue in it). If you cannot upgrade, please open your issue on StackOverflow to get further community support.

@huseyinaltas
Copy link
Author

upgraded react-native 0.75.4 and trying now

@github-actions github-actions bot added Needs: Attention Issues where the author has responded to feedback. and removed Needs: Author Feedback labels Dec 17, 2024
@huseyinaltas
Copy link
Author

0.75.4 has same issue

@sarthak-d11
Copy link
Contributor

Hi @huseyinaltas,

It would be really helpful if you could provide a sample reproducer. You can use this template to create one.

Also, I noticed that the link to your repository (Shock) is returning a 404 error, so I’m unable to access it.

Could you let me know if this issue is platform-specific (i.e., Android or iOS)? I see that you're using the old architecture—would it be possible to try with the new architecture as well?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs: Attention Issues where the author has responded to feedback. 🌐Networking Related to a networking API. Type: Unsupported Version Issues reported to a version of React Native that is no longer supported
Projects
None yet
Development

No branches or pull requests

3 participants