Skip to content

iOS: Support HTTP headers for source prop on <Image> components - #7338

Closed
rigdern wants to merge 1 commit into
react:masterfrom
rigdern:rigdern/image-headers
Closed

iOS: Support HTTP headers for source prop on <Image> components#7338
rigdern wants to merge 1 commit into
react:masterfrom
rigdern:rigdern/image-headers

Conversation

@rigdern

Copy link
Copy Markdown
Contributor

Allows developers to specify headers to include in the HTTP request
when fetching a remote image. For example, one might leverage this
when fetching an image from an endpoint that requires authentication:

<Image
style={styles.logo}
source={{
uri: 'http://facebook.github.io/react/img/logo_og.png',
headers: {
Authorization: 'someAuthToken'
}
}}
/>

Note that the header values must be strings.

Works on iOS and Android.

Test plan (required)

  • Ran a small example like the one above on iOS and Android and ensured the headers were sent to the server.
  • Ran a small example to ensure that components without headers still work.
  • Currently using this code in our app.

Adam Comella
Microsoft Corp.

@facebook-github-bot

Copy link
Copy Markdown
Contributor

By analyzing the blame information on this pull request, we identified @nicklockwood and @ide to be potential reviewers.

@facebook-github-botfacebook-github-bot added GH Review: review-needed CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. labels May 2, 2016
Comment threadLibraries/Image/Image.ios.js Outdated

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no-trailing-spaces: Trailing spaces not allowed.

@rigdern
rigdernforce-pushed the rigdern/image-headers branch from 796939f to 426ad27CompareMay 2, 2016 23:07
@ghost

ghost commented May 2, 2016

Copy link
Copy Markdown

@rigdern updated the pull request.

@rigdern
rigdernforce-pushed the rigdern/image-headers branch from 426ad27 to fed4637CompareMay 2, 2016 23:55
@ghost

ghost commented May 2, 2016

Copy link
Copy Markdown

@rigdern updated the pull request.

1 similar comment
@ghost

ghost commented May 4, 2016

Copy link
Copy Markdown

@rigdern updated the pull request.

@rigdern
rigdernforce-pushed the rigdern/image-headers branch from 1eb2054 to 32d7c77CompareMay 18, 2016 20:33
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@rigdern updated the pull request.

@rigdern
rigdernforce-pushed the rigdern/image-headers branch from 32d7c77 to 677ee37CompareMay 26, 2016 05:18
@ghost

Copy link
Copy Markdown

@rigdern updated the pull request.

Comment threadReact/Base/RCTImageSource.m Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just check if (!headers)

@ide

ide commented May 26, 2016

Copy link
Copy Markdown
Contributor

@rigdern You're awesome for submitting both Android and iOS implementations that both look good at a glance. Would like to get more reviewers on the code, but this PR looks promising.

@nicklockwood

Copy link
Copy Markdown
Contributor

@rigdern This is a good idea, but changing these iOS APIs will break quite a lot of our stuff internally.

I think the better approach here would be to add a new method to RCTImageLoader that takes an NSURLRequest, which we already have an RCTConvert method for processing in a standard format (it's already used by WebView.source). Validating that the headers are all strings should also be done inside RCTConvert (though it isn't currently).

The existing methods of RCTImageLoader can then be refactored to call the new method, so we don't break anything, and we can deprecate the old methods.

Also, while I echo @ide's sentiment about building both iOS and Android implementations, it's much easier for us to merge if it's split into separate PRs for each platform.

Allows developers to specify headers to include in the HTTP request
when fetching a remote image. For example, one might leverage this
when fetching an image from an endpoint that requires authentication:
```
<Image
style={styles.logo}
source={{
uri: 'http://facebook.github.io/react/img/logo_og.png',
headers: {
Authorization: 'someAuthToken'
}
}}
/>
```
Note that the header values must be strings.
When doing image requests, this also gives developers control over the
other properties supported by [RCTConvert NSURLRequest:] like `method`
and `body`.
This change deprecates some APIs on RCTImageLoader in order to replace
them with more flexible APIs which take an NSURLRequest rather than just
an NSURL.
@rigdern
rigdernforce-pushed the rigdern/image-headers branch from 677ee37 to ca1f5a7CompareMay 27, 2016 00:24
@ghost

Copy link
Copy Markdown

@rigdern updated the pull request.

@rigdern

Copy link
Copy Markdown
ContributorAuthor

@nicklockwood As you suggested, I created several new RCTImageLoader methods which accept an NSURLRequest and deprecated the old ones which accepted an NSURL. I also removed the Android implementation of the feature and will submit it in a separate PR.

@rigdernrigdern changed the title Support HTTP headers for source prop on <Image> componentsiOS: Support HTTP headers for source prop on <Image> componentsMay 27, 2016
@rigdern

Copy link
Copy Markdown
ContributorAuthor

Android PR is #7791

@nicklockwood

Copy link
Copy Markdown
Contributor

@facebook-github-bot import

@ghostghost closed this in ee8496fJun 1, 2016
rozele pushed a commit to microsoft/react-native-windows that referenced this pull request Jun 6, 2016
Summary:
Allows developers to specify headers to include in the HTTP request
when fetching a remote image. For example, one might leverage this
when fetching an image from an endpoint that requires authentication:
```
<Image
style={styles.logo}
source={{
uri: 'http://facebook.github.io/react/img/logo_og.png',
headers: {
Authorization: 'someAuthToken'
}
}}
/>
```
Note that the header values must be strings.
Works on iOS and Android.
**Test plan (required)**
- Ran a small example like the one above on iOS and Android and ensured the headers were sent to the server.
- Ran a small example to ensure that \<Image\> components without headers still work.
- Currently using this code in our app.
Adam Comella
Microsoft Corp.
Closesreact/react-native#7338
Reviewed By: javache
Differential Revision: D3371458
Pulled By: nicklockwood
fbshipit-source-id: cdb24fe2572c3ae3ba82c86ad383af6d85157e20
@rgoldiez

rgoldiez commented Jul 29, 2016

Copy link
Copy Markdown

@rigdern - Did this PR get accepted and, if so, which version of RN did it first appear in?

EDIT -- nevermind... I see it was accepted.

@bitfused

Copy link
Copy Markdown

Are there any working examples? This is not working for me and I am not sure how to debug this..

@rgoldiez

Copy link
Copy Markdown

@IlyaRepo You want to do something like this, which is working for me

<Image source={{
uri: url,
headers: {
'Authorization': someToken, // or whatever your header looks like
}
}}
/>

facebook-github-bot pushed a commit that referenced this pull request Apr 19, 2017
Summary:
Hi there,
As ospfranco found there is some documentation for the code coming from #7338 missing, so I added it. It concerns the method, headers and bdoy field on the Image source object.
If you would like to have any changes made, please don't hesitate to comment, I will add them.
Have a nice day and thank you for maintaining React Native!
Closes#9304
Differential Revision: D4913262
Pulled By: javache
fbshipit-source-id: 922430ec3388560686e1cf53cb5dff7f30e4e31f
Maxwell2022 pushed a commit to Maxwell2022/react-native that referenced this pull request Apr 19, 2017
Summary:
Hi there,
As ospfranco found there is some documentation for the code coming from react#7338 missing, so I added it. It concerns the method, headers and bdoy field on the Image source object.
If you would like to have any changes made, please don't hesitate to comment, I will add them.
Have a nice day and thank you for maintaining React Native!
Closesreact#9304
Differential Revision: D4913262
Pulled By: javache
fbshipit-source-id: 922430ec3388560686e1cf53cb5dff7f30e4e31f
@xahon

Copy link
Copy Markdown

Headers not working on ios, android works well

Trancever pushed a commit to Trancever/react-native-image-editor that referenced this pull request Feb 12, 2019
Summary:
Allows developers to specify headers to include in the HTTP request
when fetching a remote image. For example, one might leverage this
when fetching an image from an endpoint that requires authentication:
```
<Image
style={styles.logo}
source={{
uri: 'http://facebook.github.io/react/img/logo_og.png',
headers: {
Authorization: 'someAuthToken'
}
}}
/>
```
Note that the header values must be strings.
Works on iOS and Android.
**Test plan (required)**
- Ran a small example like the one above on iOS and Android and ensured the headers were sent to the server.
- Ran a small example to ensure that \<Image\> components without headers still work.
- Currently using this code in our app.
Adam Comella
Microsoft Corp.
Closesreact/react-native#7338
Reviewed By: javache
Differential Revision: D3371458
Pulled By: nicklockwood
fbshipit-source-id: cdb24fe2572c3ae3ba82c86ad383af6d85157e20
bartolkaruza pushed a commit to bartolkaruza/react-native-cameraroll that referenced this pull request Feb 24, 2019
Summary:
Allows developers to specify headers to include in the HTTP request
when fetching a remote image. For example, one might leverage this
when fetching an image from an endpoint that requires authentication:
```
<Image
style={styles.logo}
source={{
uri: 'http://facebook.github.io/react/img/logo_og.png',
headers: {
Authorization: 'someAuthToken'
}
}}
/>
```
Note that the header values must be strings.
Works on iOS and Android.
**Test plan (required)**
- Ran a small example like the one above on iOS and Android and ensured the headers were sent to the server.
- Ran a small example to ensure that \<Image\> components without headers still work.
- Currently using this code in our app.
Adam Comella
Microsoft Corp.
Closesreact/react-native#7338
Reviewed By: javache
Differential Revision: D3371458
Pulled By: nicklockwood
fbshipit-source-id: cdb24fe2572c3ae3ba82c86ad383af6d85157e20
This pull request was closed.
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA SignedThis label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants

@rigdern@facebook-github-bot@ide@nicklockwood@rgoldiez@bitfused@xahon@eslint-bot