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

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

Closed
wants to merge 1 commit into from

Conversation

rigdern
Copy link
Contributor

@rigdern rigdern commented May 2, 2016

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
Contributor

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

@facebook-github-bot facebook-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
@@ -63,10 +63,14 @@ var Image = React.createClass({
* `uri` is a string representing the resource identifier for the image, which
* could be an http address, a local file path, or the name of a static image
* resource (which should be wrapped in the `require('./path/to/image.png')` function).
*

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.

@ghost
Copy link

ghost commented May 2, 2016

@rigdern updated the pull request.

@ghost
Copy link

ghost commented May 2, 2016

@rigdern updated the pull request.

1 similar comment
@ghost
Copy link

ghost commented May 4, 2016

@rigdern updated the pull request.

@facebook-github-bot
Copy link
Contributor

@rigdern updated the pull request.

@ghost
Copy link

ghost commented May 26, 2016

@rigdern updated the pull request.

CGSize size = CGSizeZero;
CGFloat scale = 1.0;
BOOL packagerAsset = NO;
if ([json isKindOfClass:[NSDictionary class]]) {
if (!(imageURL = [self NSURL:RCTNilIfNull(json[@"uri"])])) {
return nil;
}
headers = [self NSDictionary:RCTNilIfNull(json[@"headers"])];
#if RCT_DEBUG
if (headers != nil) {
Copy link
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
Copy link
Contributor

ide commented May 26, 2016

@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
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.
@ghost
Copy link

ghost commented May 27, 2016

@rigdern updated the pull request.

@rigdern
Copy link
Contributor Author

rigdern commented May 27, 2016

@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.

@rigdern rigdern changed the title Support HTTP headers for source prop on <Image> components iOS: Support HTTP headers for source prop on <Image> components May 27, 2016
@rigdern
Copy link
Contributor Author

rigdern commented May 27, 2016

Android PR is #7791

@nicklockwood
Copy link
Contributor

@facebook-github-bot import

@ghost ghost closed this in ee8496f Jun 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.
Closes facebook/react-native#7338

Reviewed By: javache

Differential Revision: D3371458

Pulled By: nicklockwood

fbshipit-source-id: cdb24fe2572c3ae3ba82c86ad383af6d85157e20
@rgoldiez
Copy link

rgoldiez commented Jul 29, 2016

@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.

samerce pushed a commit to iodine/react-native that referenced this pull request Aug 23, 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.
Closes facebook#7338

Reviewed By: javache

Differential Revision: D3371458

Pulled By: nicklockwood

fbshipit-source-id: cdb24fe2572c3ae3ba82c86ad383af6d85157e20
mpretty-cyro pushed a commit to HomePass/react-native that referenced this pull request Aug 25, 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.
Closes facebook#7338

Reviewed By: javache

Differential Revision: D3371458

Pulled By: nicklockwood

fbshipit-source-id: cdb24fe2572c3ae3ba82c86ad383af6d85157e20
@bitfused
Copy link

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

@rgoldiez
Copy link

@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
      }
     }}
     />

aleclarson pushed a commit to aleclarson/react-native that referenced this pull request Sep 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.
Closes facebook#7338

Reviewed By: javache

Differential Revision: D3371458

Pulled By: nicklockwood

fbshipit-source-id: cdb24fe2572c3ae3ba82c86ad383af6d85157e20
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 facebook#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 facebook#9304

Differential Revision: D4913262

Pulled By: javache

fbshipit-source-id: 922430ec3388560686e1cf53cb5dff7f30e4e31f
thotegowda pushed a commit to thotegowda/react-native that referenced this pull request May 7, 2017
Summary:
Hi there,

As ospfranco found there is some documentation for the code coming from facebook#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 facebook#9304

Differential Revision: D4913262

Pulled By: javache

fbshipit-source-id: 922430ec3388560686e1cf53cb5dff7f30e4e31f
@xahon
Copy link

xahon commented Aug 31, 2017

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.
Closes facebook/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.
Closes facebook/react-native#7338

Reviewed By: javache

Differential Revision: D3371458

Pulled By: nicklockwood

fbshipit-source-id: cdb24fe2572c3ae3ba82c86ad383af6d85157e20
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This 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