Cannot read from Inputstream (Gio.Inputstream)

I have the following code

let session = new Soup.Session();
const message = Soup.Message.new(‘GET’,‘https://www.example.com’);
let inputStream = session.send_message(message);
let [returnval,bffr] = inputStream.read(null);

and the error I get is,

(gjs:4338): Gjs-CRITICAL **: 10:17:17.987: JS ERROR: TypeError: inputStream.read is not a function
@hello.js:42:32

Thank you.

Never worked with GJS before, so probably others can give better answers.

Are you using libsoup2 or libsoup3? Assuming libsoup2, it seems that Soup.Session.send_message() returns a Number, whereas Soup.Session.send() returns a Gio.InputStream.

You may try to inspect the returned objects using e.g console.log(JSON.stringify()) or the new prettyPrint: Verbose object print output (!587) · Merge requests · GNOME / gjs · GitLab

1 Like

Hello,

We have a GJS example here examples/http-client.js · sonny-master-patch-71423 · GNOME / gjs · GitLab

And an other one: Workbench/main.js at fa40deb025adb1a78a6b853ec00ae4c9ce1678f4 · sonnyp/Workbench · GitHub

2 Likes

I am using soup 2.0. I am trying to read from a RSS feed. If the response content type is application/xml the soup.session.send(msg, cancellable ) works which returns Gio.Inputstram. But when the response content type is application/rss+xml soup.session.send(msg, cancellable ) doesn’t work. soup.send_message(msg) works with both the cases.

As per the API documentation, the soup.send(msg,cancellable) synchronously sends a message, but returns before reading the response body, and allows you to read the response via Gio.InputStream.
I don’t know the difference between application/xml and application/rss+xml content type.

Thank you.

I have gone through those examples. I am a beginner. Just going through the API documentation and trying to learn.

1 Like

If the response content type is application/xml the soup.session.send(msg, cancellable ) works which returns Gio.Inputstram. But when the response content type is application/rss+xml soup.session.send(msg, cancellable ) doesn’t work.

That seems unlikely.

If you need further help, consider sharing a .js reproducer I can run with GJS, I will have a look at it.

Yes, you are right it does work. It was my mistake. I couldn’t upload .js file here so, I am just copy pasteing my code.
/********************************************************************************
const {Soup,GLib, Gio} = imports.gi;

let session = new Soup.Session();

// content type application/xml
//const message = Soup.Message.new(‘GET’, ‘https://www.yahoo.com/news/rss’);

// content type application/rss+xml
const message = Soup.Message.new(‘GET’, ‘Pune | The Indian Express’);

let inputStream = session.send(message,null);

const newread = Gio.DataInputStream.new (inputStream);

let str = newread.read_line_utf8(null);

print (str);

/**************************************************************************************

If you can help me understand, why
let [num, b_array] = inputStream.read();
gives me an error,

JS ERROR: TypeError: method Gio.InputStream.read: At least 2 arguments required, but only 1 passed
@client_set_response.js:15:34

Thank you for your help!!

Gio.InputStream.read() needs a Gio.Cancellable argument.

The error is a bit confusing, but let [nRead, bArray] = inputStream.read(null) should work.

Yes, I passed the null object and I get the same error.

I am using Ubuntu 22.04.2.
Gnome shell version is 42.5.
gjs version is 1.72.2.

Do I have to check the development libraries I am using ? If so, how can I do that?

Ah, sorry I forgot this is an existing issue in GJS (see #501).

In this case, you’ll just have to use a slightly higher-level method like Gio.InputStream.read_bytes().

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.