Python Forum
GET Request and JSON response - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Networking (https://python-forum.io/forum-12.html)
+--- Thread: GET Request and JSON response (/thread-13001.html)



GET Request and JSON response - montyonthebonty - Sep-23-2018

Good morning

I'm trying to use Python to connect to a MySQL Database, so I'm trying to use a GET request and a PHP script.

The code I have so far is:
import requests
import json

print 'Hello, World'


url = 'https://www.thebar.inthepub.co.uk/resttest.php'

response = requests.get(url)

dict = response.json()

print (dict)
This then links to a file with the following:
<?php

  echo '<p>test</p>';

$myObj->name = "John";
$myObj->age = 30;
$myObj->city = "New York";

$myJSON = json_encode($myObj);

echo $myJSON;


?>
But when I run it, I get an error, 'No JSON Object could be decoded'. Any ideas why? Is it my code, or is there another module I need to install?

Thanks
Chris


RE: GET Request and JSON response - wavic - Sep-23-2018

See the documentation.

Are you sure that the get method returns a valid JSON object? What is the print printing?


RE: GET Request and JSON response - montyonthebonty - Sep-23-2018

Thanks for replying. I thought the extra print statements for debugging might be confusing things, but I have taken them out and I still get the same error.

The print statement gives the following:

{"name":"John","age":30,"city":"New York"}

Thanks
Chris


RE: GET Request and JSON response - wavic - Sep-23-2018

It looks to me like a dictionary.
Post the full error traceback between error tags, please.


RE: GET Request and JSON response - montyonthebonty - Sep-23-2018

Good afternoon

Error message in full:

Hello, World
Traceback (most recent call last):
File "resttest.py", line 11, in <module>
dict = response.json()
File "/usr/local/lib/python2.7/site-packages/requests/models.py", line 896, in json
return complexjson.loads(self.text, **kwargs)
File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 339, in loads
return _default_decoder.decode(s)
File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 382, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

Thanks
Chris


RE: GET Request and JSON response - wavic - Sep-23-2018

When I open the web page a captcha appears. It's not returning JSON at once.


RE: GET Request and JSON response - snippsat - Sep-23-2018

You get ValueError: No JSON object could be decoded,because you do not get json back.
Get 403.
>>> response
<Response [403]>
>>> print(response.text)
...
If you look at what's in text.
<p>Access to this resource on the server is denied!</p>
It work if add header with user-agent.
Do not use dict as variable name,as it's a python reserved word.
import requests

url = 'https://www.thebar.inthepub.co.uk/resttest.php'
headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
}
response = requests.get(url, headers=headers)
res_json = response.json()
print(res_json)
Output:
{'name': 'John', 'age': 30, 'city': 'New York'}



RE: GET Request and JSON response - montyonthebonty - Sep-23-2018

Thanks, that's brilliant - I have it working now. Just one question though - when I run the program in Terminal, the output I get is this:

{u'city': u'New York', u'age': 30, u'name': u'John'}

I've tried accessing the dictionary using name, age etc. and it works fine, giving the same responses. Just wondered why it might be adding the random 'u's?

I don't think it matters - it's working okay, just curious!

Thanks
Chris


RE: GET Request and JSON response - snippsat - Sep-23-2018

(Sep-23-2018, 06:17 PM)montyonthebonty Wrote: Just wondered why it might be adding the random 'u's?
You get that because you use Python 2,you should use python 3.6 -->.
u'something' show that's it s Unicode string in python 2,in Python 3 so is all string Unicode bye default.


RE: GET Request and JSON response - buran - Sep-27-2018

also, don't use dict as variable name. it's built-in function