Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

authentication - Gnome Shell Extension: Send Request with Authorization Bearer Headers

I am trying to build a gnome shell extension (using gjs) that I need to communicate with an external REST API. In order to do so, I need to accompany my requests with the header: Authorization: Bearer <token> and with a Content-Type: application/json.

I have looked all over for questions like this and I did find some similar ones but none of them works. The documentation is not helpful at all, and, if anything, it has only confused me more.

With curl I could send that request as follows:

curl -X GET -H "Authorization: Bearer <token>" -H "Content-Type: application/json" <url>

So far, I have only created extensions that send simple GET requests with no headers. Then I would do the following:

const Soup = imports.gi.Soup;
let soupSyncSession = new Soup.SessionSync();

let message = Soup.Message.new('GET', url);
let responseCode = soupSyncSession.send_message(message);
let res;
if(responseCode == 200) {
    res = JSON.parse(message['response-body'].data);
}

Any idea on how I can add the headers? Any help would be appreciated!

question from:https://stackoverflow.com/questions/65830466/gnome-shell-extension-send-request-with-authorization-bearer-headers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

message.request_headers is a Soup.MessageHeaders object to which you can append() the authorization and content type headers.

Additionally there is a convenient set_content_type() method for the content type header specifically.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...