# Http\_open and repositioning the stream

**URL:** <https://swi-prolog.discourse.group/t/http-open-and-repositioning-the-stream/2236>\
**Category:** General\
**Created:** [April 26, 2020, 7:09pm UTC](https://swi-prolog.discourse.group/t/http-open-and-repositioning-the-stream/2236 "2020-04-26T19:09:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![esad](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/esad/32/884_2.png) [@esad](https://swi-prolog.discourse.group/u/esad)\
**Post date:** [April 26, 2020, 7:09pm UTC](https://swi-prolog.discourse.group/t/http-open-and-repositioning-the-stream/2236/1 "2020-04-26T19:09:21Z")

</div>

When using `seek/4` on a stream obtained via `http_open` I get weird behaviour.

If I use `read_string` to just read a bit from it, I’m able to rewind it and read again:

So:

```
http_open("https://www.swi-prolog.org/", S, []), read_string(S, 9, Str), seek(S, 0, bof, _), read_string(S, 9, Str).

```

unifies. However, if I read the stream until the end, it throws the `No permission to reposition stream` exception:

```
http_open("https://www.swi-prolog.org/", S, []), read_string(S, _, Str), seek(S, 0, bof, _), read_string(S, _, Str).

```

Is there some kind of flag set that disables repositioning once eof is reached and can I change this behaviour?

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [April 26, 2020, 7:29pm UTC](https://swi-prolog.discourse.group/t/http-open-and-repositioning-the-stream/2236/2 "2020-04-26T19:29:43Z")

</div>

HTTP streams are sockets and not repositionable. You can be lucky if the seek is inside the current buffer. Possibly peek\_string/3 can solve your problem?

---

<div class="post-metadata">

**Author:** ![esad](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/esad/32/884_2.png) [@esad](https://swi-prolog.discourse.group/u/esad)\
**Post date:** [April 26, 2020, 7:34pm UTC](https://swi-prolog.discourse.group/t/http-open-and-repositioning-the-stream/2236/3 "2020-04-26T19:34:29Z")

</div>

I think `peek_string/3` could work, but it can’t be used to peek until the end, that is the length must be supplied/known beforehand. Right now I am working around this by using `read_string/3` to read the raw response, then reopening it with `open_string/2` and passing that stream to json parser.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [April 26, 2020, 7:35pm UTC](https://swi-prolog.discourse.group/t/http-open-and-repositioning-the-stream/2236/4 "2020-04-26T19:35:48Z")

</div>

There is no limit on how long you can peek. It does cost memory though. If the input is really long it is probably best to first send to a file.
