# HTTP headers when opening a stream

**URL:** <https://forums.plex.tv/t/http-headers-when-opening-a-stream/142543>\
**Category:** Dev/API Corner\
**Tags:** plugin-dev\
**Created:** [April 16, 2016, 9:37pm UTC](https://forums.plex.tv/t/http-headers-when-opening-a-stream/142543 "2016-04-16T21:37:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![txt3rob](https://avatars.discourse-cdn.com/v4/letter/t/9de0a6/32.png) [@txt3rob](https://forums.plex.tv/u/txt3rob)\
**Post date:** [April 16, 2016, 9:37pm UTC](https://forums.plex.tv/t/http-headers-when-opening-a-stream/142543/1 "2016-04-16T21:37:39Z")

</div>

Hey,

Just looking for some help.

When plex opens a HLS stream is there any way to add a x-forwarded-header when a device opens the stream?

I can add the usual headers etc to open webpages etc but not when it comes to the stream i want the device to open the stream with that header set.

i tried adding the header on the object container but it does not appear to be passing it.

oc = ObjectContainer(http\_headers={‘X-Forwarded-For’:‘127.0.0.1’})

I’ve been using burp to check

---

<div class="post-metadata">

**Author:** ![coryo123](https://avatars.discourse-cdn.com/v4/letter/c/a587f6/32.png) [@coryo123](https://forums.plex.tv/u/coryo123)\
**Post date:** [April 16, 2016, 11:37pm UTC](https://forums.plex.tv/t/http-headers-when-opening-a-stream/142543/2 "2016-04-16T23:37:53Z")

</div>

If the clients don’t respect the http\_headers parameter you could try having the server act as a proxy for the playlist.

something like this might work in a url service:

```auto
def MediaObjectsForURL(url):
    return [
        MediaObject(
            protocol='hls',
            container='mpegts',
            video_codec=VideoCodec.H264,
            audio_codec=AudioCodec.AAC,
            audio_channels=2,
            optimized_for_streaming=True,
            parts=[PartObject(key=Callback(playlist, url=url, ext='m3u8'))]
        )
    ]

def playlist(url):
    return HTTP.Request(url, headers={'X-Headers': '...'}).content

```

---

<div class="post-metadata">

**Author:** ![Twoure](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/twoure/32/91016_2.png) [@Twoure](https://forums.plex.tv/u/Twoure)\
**Post date:** [April 17, 2016, 5:33am UTC](https://forums.plex.tv/t/http-headers-when-opening-a-stream/142543/3 "2016-04-17T05:33:29Z")

</div>

Depending on what you need and how you setup your `MediaObjectsForURL` function you can include the `http_headers` field within a `VideoClipObject` and/or the `PartObject`.

Examples [iTunes Movie Trailers](https://github.com/plexinc-plugins/Services.bundle/blob/8afdf0b36754683d9574623b314b5579ed5ead49/Contents/Service%20Sets/com.plexapp.plugins.amt/URL/iTunes%20Movie%20Trailers/ServiceCode.pys#L104), [ForaTV](https://github.com/plexinc-plugins/Services.bundle/blob/8afdf0b36754683d9574623b314b5579ed5ead49/Contents/Service%20Sets/com.plexapp.plugins.foratv/URL/ForaTV/ServiceCode.pys#L64) (Note: ForaTV is doing basically the same thing as you already tried),

```auto
def MediaObjectsForURL(url):
    return [
        MediaObject(
            parts=[PartObject(
                key=HTTPLiveStreamURL(Callback(PlayVideo, url=url))
                )]
            )
        ]

@indirect
def PlayVideo(url, **kwargs): 
    """
    do the things
    Parse for m3u8_url and set video headers
    """
    headers={'X-Forwarded-For': '127.0.0.1'}
    return IndirectResponse(VideoClipObject, key=m3u8_url, http_headers=headers)

```

If you do not want to use the Callback method within the `PartObject`, you should be able to set the `PartObject` `key` to the stream URL and set the `http_headers` to your `{'X-Forwarded-For': '127.0.0.1'}`,

```auto
def MediaObjectsForURL(url):
    headers={'X-Forwarded-For': '127.0.0.1'}
    return [
        MediaObject(
            parts=[PartObject(
                key=HTTPLiveStreamURL(url), http_headers=headers
                )]
            )
        ]

```

If you leave out the `HTTPLiveStreamURL` function, then you will need to include the information outlined in [Comment\_1168292](https://forums.plex.tv/discussion/comment/1168292/#Comment_1168292), so Plex can handle the HLS stream correctly.

All of the above can be found within the `objectkit.py` file within the following directory:

```auto
Framework.bundle/Contents/Resources/Versions/2/Python/Framework/api

```

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/plex/original/3X/2/a/2acb9765406f63293d357b4ec509ec39aa28f2ad.png) [@system](https://forums.plex.tv/u/system)\
**Post date:** [January 8, 2020, 7:56pm UTC](https://forums.plex.tv/t/http-headers-when-opening-a-stream/142543/4 "2020-01-08T19:56:59Z")

</div>


