![]() |
#1 |
Enthusiast
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 29
Karma: 10545
Join Date: May 2024
Device: none
|
self.browser.open_novisit() POST with custom header ?
since as a plugin developer i have to use the library versions that Caliber include,
how do i call a post with a custom header ? i tried: PHP Code:
PHP Code:
PHP Code:
anyone knows ? |
![]() |
![]() |
![]() |
#2 |
creator of calibre
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 44,289
Karma: 23661992
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
You are free to distribute any libraries you want to use with your plugin. And mechanize is extensively documented. Use its docs.
|
![]() |
![]() |
Advert | |
|
![]() |
#3 |
Enthusiast
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 29
Karma: 10545
Join Date: May 2024
Device: none
|
i did use it's docs, these are the options it gave, and it does not work, (the code is working, no problem in installing and executing the metadata plugin, but its not impacting the request headers)
i also tried adding requests, but that uses importlib, which is harder to trick with this "calibre_plugins.evrit2." prefix trick is there any working solution example ? |
![]() |
![]() |
![]() |
#4 |
creator of calibre
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 44,289
Karma: 23661992
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
Let me ask chatgpt for you
![]() To perform a POST request using Python's mechanize, you can follow these steps: Import the necessary modules. Create an instance of mechanize.Browser. Prepare the data you want to send in the POST request. Use the open() method of the Browser instance to send the POST request. Here's an example based on the information provided in the sources: import mechanize import urllib.parse # Create a browser instance browser = mechanize.Browser() # Define the URL for the POST request post_url = 'http://example.com/login.php' # Prepare the data to be sent parameters = { "username": "avi", "password": "stackoverflow" } # Encode the data encoded_data = urllib.parse.urlencode(parameters) # Send the POST request response = browser.open(post_url, data=encoded_data) # Read and print the response content print(response.read()) Though really, all due respect (which is not much) to chatgpt, the correct way to make post requests is to create a mechanize Request object with method="POST". See for example hbr.recipe in calibre's sources. And you dont need to trick requests into anything. Store its whl file in your plugin zip file, extract it to filesystem and import from there. |
![]() |
![]() |
![]() |
#5 |
Enthusiast
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 29
Karma: 10545
Join Date: May 2024
Device: none
|
never mind, i gave up and used http.client,
b.t.w. i hope after 40 years of experience i would know how to use ChatGPT (i develop LLMs), but if you pay attention you will see that there is no custom headers in the answer it gave you, but thanks anyway |
![]() |
![]() |
Advert | |
|
![]() |
#6 |
Enthusiast
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 29
Karma: 10545
Join Date: May 2024
Device: none
|
SOLVED! post request with custom headers from within a metadata plugin
for any developer that might encounter this issue, you better use http.vlient in your plugin
for rest api calls with post json payload here is a working example: PHP Code:
Last edited by dandman; 05-17-2024 at 11:57 PM. |
![]() |
![]() |
![]() |
#7 |
creator of calibre
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 44,289
Karma: 23661992
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
You develop LLMs and yet you dont seem to know how to use them??? Here let me ask it to include headers and use Request for you
Code:
import mechanize import urllib.parse # Create a browser instance browser = mechanize.Browser() # Define the URL for the POST request post_url = 'http://example.com/login.php' # Prepare the data to be sent parameters = { "username": "avi", "password": "stackoverflow" } # Encode the data encoded_data = urllib.parse.urlencode(parameters) # Setup custom headers custom_headers = [ ('Accept', 'text/javascript, text/html, application/xml, text/xml, */*'), ('Content-type', 'application/x-www-form-urlencoded; charset=UTF-8'), ('User-Agent', 'Foobar'), ] # Create a mechanize.Request object with custom headers request = mechanize.Request(post_url, data=encoded_data, headers=dict(custom_headers), method="POST") # Send the POST request with the custom request object response = browser.open(request) # Read and print the response content print(response.read()) |
![]() |
![]() |
![]() |
#8 |
creator of calibre
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 44,289
Karma: 23661992
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
And I searched the mechanize docs for POST and here we are:
https://mechanize.readthedocs.io/en/...hanize.Request |
![]() |
![]() |
![]() |
Tags |
custom header, headers, plugin development |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Convert to PDF - Custom Header Not Working | 12345joy | Conversion | 1 | 11-28-2022 12:00 PM |
[Browser Viewer] Current Section disappearing from Header/Footer | nqk | Viewer | 0 | 12-07-2020 09:22 AM |
In epub converting calibre auto split file in header. Why not create toc from header. | The_book | Conversion | 7 | 11-06-2020 09:09 AM |
Y/N custom column in Tag Browser? | matthewdeans | Calibre | 15 | 08-12-2011 02:01 PM |
Kindle browser header | TadW | Kindle Developer's Corner | 6 | 02-18-2008 03:08 AM |