Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Development

Notices

Reply
 
Thread Tools Search this Thread
Old 05-17-2024, 07:52 PM   #1
dandman
Enthusiast
dandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watch
 
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:
self.browser.set_current_header(('Content-Type''application/json; charset=utf-8'))
response self.browser.open_novisit('https://www......'data=json.dumps(json_object), timeout=5
and:

PHP Code:
self.browser.addheaders += [('Content-Type''application/json; charset=utf-8')]
response self.browser.open_novisit('https://www......'data=json.dumps(json_object), timeout=5
and:

PHP Code:
self.browser.addheaders = [('Content-Type''application/json; charset=utf-8')]
response self.browser.open_novisit('https://www......'data=json.dumps(json_object), timeout=5

anyone knows ?
dandman is offline   Reply With Quote
Old 05-17-2024, 11:50 PM   #2
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 44,542
Karma: 24495948
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.
kovidgoyal is offline   Reply With Quote
Advert
Old 05-18-2024, 12:05 AM   #3
dandman
Enthusiast
dandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watch
 
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 ?
dandman is offline   Reply With Quote
Old 05-18-2024, 12:11 AM   #4
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 44,542
Karma: 24495948
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.
kovidgoyal is offline   Reply With Quote
Old 05-18-2024, 12:25 AM   #5
dandman
Enthusiast
dandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watch
 
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
dandman is offline   Reply With Quote
Advert
Old 05-18-2024, 12:53 AM   #6
dandman
Enthusiast
dandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watch
 
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:
import http.client
import json

# post a json body to a url https://www.example.com/api/GetBookDetails in order to fetch books details
connection None
post_domain 
'example.com'
post_url '/api/GetBookDetails'
post_headers = {'Content-Type''application/json; charset=utf-8'}
post_body = {'Books': [{'BookID'1234}]}
try:
    
# connect to the server within 5 seconds timeout period
    
connection http.client.HTTPSConnection(post_domaintimeout=5)
    
# perform the POST request with the json payload
    
connection.request("POST"post_urljson.dumps(post_body), post_headers)
    
# get the response from the server
    
response connection.getresponse()

    
# make sure the server returned 200, OK response (and not an error)
    
if response.getcode() == 200:
        
# read the response text and decode it as utf-8
        
response_text response.read().decode('utf-8')
        
# parse the response as a json object
        
try:
            
json_book_details json.loads(response_text)
            
# print the json object
            
print(json.dumps(json_book_detailsindent=4))
        
except json.JSONDecodeError as e:
            print(
'Error parsing json response: 'e)
except Exception as e:
    print(
'General error occurred while fetching book details:'e)
finally:
    
# close the connection
    
if connection:
        
connection.close() 
change the url parts (domain, url) to match your site configuration

Last edited by dandman; 05-18-2024 at 12:57 AM.
dandman is offline   Reply With Quote
Old 05-18-2024, 01:02 AM   #7
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 44,542
Karma: 24495948
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())
It's freaking awful code, as one would expect from an LLM, but it shows you how to do it, which also you would have found if you had followed my previous tip to look at hbr.recipe
kovidgoyal is offline   Reply With Quote
Old 05-18-2024, 01:08 AM   #8
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 44,542
Karma: 24495948
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
kovidgoyal is offline   Reply With Quote
Reply

Tags
custom header, headers, plugin development


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert to PDF - Custom Header Not Working 12345joy Conversion 1 11-28-2022 01:00 PM
[Browser Viewer] Current Section disappearing from Header/Footer nqk Viewer 0 12-07-2020 10:22 AM
In epub converting calibre auto split file in header. Why not create toc from header. The_book Conversion 7 11-06-2020 10:09 AM
Y/N custom column in Tag Browser? matthewdeans Calibre 15 08-12-2011 03:01 PM
Kindle browser header TadW Kindle Developer's Corner 6 02-18-2008 04:08 AM


All times are GMT -4. The time now is 05:36 AM.


MobileRead.com is a privately owned, operated and funded community.