I think your code sends query json as application/x-www-form-urlencoded and not 
as json in the post request body. In the first case the query should be in q 
parameter as non-json similar to a get request. If you want to use the json 
query api then content type must be application/json

-ufuk

—

> On Dec 17, 2024, at 19:23, Andrew Hankinson <andrew.hankinson@rism.digital> 
> wrote:
> 
> You're not actually POSTing the data. 
> 
> The requests module is a lot easier than using the urllib module.
> 
> https://requests.readthedocs.io/en/latest/
> 
> r = requests.post('http://localhost:8983/solr/films/query', 
> data={"query":"*:*"})
> print(r.json())
> 
> This should set all the appropriate headers as well.
> 
> -Andrew
> 
>> On 17 Dec 2024, at 10:53, Macfarlane, Andrew 
>> <a.macfarlan...@city.ac.uk.INVALID> wrote:
>> 
>> Hi
>> 
>> I’m trying to connect with a SOLR server, using the POST methods in python, 
>> but I’m getting an error.
>> 
>> This is the code (full file is attached):
>> 
>> #Form the URL
>> searchURL  = 'http://localhost:8983/solr/films/query'
>> 
>> querytext = {"json":{"query":"shane"}}
>> #querytext = {"query":"*:*"}
>> 
>> #form the query
>> query = urllib.parse.urlencode(querytext).encode('utf-8')
>> 
>> #get data from the server
>> try: 
>>  connection = urllib.request.Request(searchURL, query)
>> except Exception as error:
>>  print("Connection failed: ", error)
>>  exit()
>> 
>> connection.add_header('Content-Type', 'application/json')
>> 
>> #extract data from the connection made
>> try: 
>>  response = urllib.request.urlopen(connection)
>> except Exception as error:
>>  print("No response returned: ", error)
>>  exit()
>> 
>> Reponse is:
>> 
>> solr-9.6.1>python python-apps\film-POST.py
>> No response returned:  HTTP Error 400: Bad Request
>> 
>> Logging on the server gives:
>> 
>> <image001.png>
>> I’ve attempted many ways resolving this to address the issue, but with no 
>> success. Can someone help me please?
>> 
>> Cheers
>> Andy 
>> 
>> --------------------------------------------------
> 

Reply via email to