View Single Post
Old 05-18-2024, 12: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: 45,445
Karma: 27757438
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