LzxHahaha
LzxHahaha

Reputation: 47

How to get a response header when load an image in react native?

The same url will return different image(random), and I need to get the response's header(also will be different in each times), so I can not fetch twice.

I try to use the blob, but get a warn that said 'blob' is undefined, the code like this:

let response = await fetch(URLs.host + URLs.imageCode);
let key = response.headers.get('key');
console.log(response.blob); // this will print 'undefined'
let blob = await response.blob();
this.setState({source: URL.createObjectURL(blob)});

...

<Image source={{uri: this.state.source}} />

So how can I get the header when load the image?

Upvotes: 0

Views: 1725

Answers (2)

ssomnoremac
ssomnoremac

Reputation: 759

You can't use fetch to get a blob in React-Native per this because fetch is a native http call that stores the blob on the native level and doesn't provide a reference to the actual memory to the javaScript blob object in res.blob()

Best option until someone writes a bridge for this is to use base64 encoded images which is just a string so you can get it with res.text()

Upvotes: 1

LzxHahaha
LzxHahaha

Reputation: 47

Well, I found now we can not get the context when load an image by use {{uri: xxx}} in react-native, so my solution is talk to the server develper and let they design the API in an other way.

Upvotes: 0

Related Questions