REST requests through pyRevit

REST requests through pyRevit

Anonymous
Not applicable
2,149 Views
5 Replies
Message 1 of 6

REST requests through pyRevit

Anonymous
Not applicable

Hi all,

I'm trying to create a pyRevit button which uses some information I'm getting from a website api using REST requests.

All is working fine when I'm just requesting and running the code on Visual Studio Code, but once I try to turn the whole thing into a button it seams like it doesn't recognize the 'requests' module.


I've been searching and searching but didn't find any information about how to import a module pyRevit doesn't recognize automatically or about a different inherent module suitable for my needs.

Would really appreciate any advise about the matter.

 

I have attached a print screen of the code.

 

Best,

Ifat.

0 Likes
Accepted solutions (1)
2,150 Views
5 Replies
Replies (5)
Message 2 of 6

bhprest
Advocate
Advocate

Is the requests module something that you built yourself?

 

If not, have you researched as to whether the requests module you're using is compatible with ironpython?

 

There's not much more I can say without digging through your code. Having worked with pyRevit and revitpythonshell in the past, a very common issue is compatibility with the ironpython implementation. 

 

0 Likes
Message 3 of 6

Anonymous
Not applicable

Hi,
Thanks for the reply.

No, I haven't built it myself.
BUT in the meanwhile I managed to figure out how to use WebRequest to get the info from the API, now I'm struggling to understand how to post info back to the api 🙂

There isn't enough solid info about using WebRequest with IronPython out there, at least not enough for a newbie...

 

Best,

Ifat.

 

0 Likes
Message 4 of 6

Anonymous
Not applicable
Accepted solution

Ok found it! 🙂

Found this link: 

http://www.ironpython.info/index.php?title=Submit_a_POST_form_and_download_the_result_web_page

 

After lot's of trial and error I managed to figure out how to adjust it to my needs.

If you need to add headers to a request, you'll have to create a WebHeaderCollection and add the header to it like you would to a dictionary:

    whc = WebHeaderCollection()
    whc[aut] = tok
    req = WebRequest.Create(url)
    req.Headers = whc

 

 Then you proceed with the example from the link.

 

Best,

Ifat.

 

Message 5 of 6

Ian_H
Advocate
Advocate

I think I am in a similar situation as you were. I'm not wanting to pull any info from Revit, I'm just wanting to send data to Notion. I have a working script in VS Code, but when I placed it in my folder structure it didn't work. 

 

I think it has to do with the imported libraries and maybe python3 vs ironpython. The libraries I'm using are 'requests' and 'json'. The link you posted didn't work, do I just need to follow the instructions you provided for the script to work? 

 

Here is the GitHub script link if you want to view the code for reference-->

https://github.com/kslifter/Python

 

Thanks!

0 Likes
Message 6 of 6

yanirKC3ZG
Participant
Participant

the github link or this link
http://www.ironpython.info/index.php?title=Submit_a_POST_form_and_download_the_result_web_page
are not working, can you attach example code so request will work?

0 Likes