Python Forum
GET Request and JSON response
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
GET Request and JSON response
#1
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
Reply
#2
See the documentation.

Are you sure that the get method returns a valid JSON object? What is the print printing?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
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
Reply
#4
It looks to me like a dictionary.
Post the full error traceback between error tags, please.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
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
Reply
#6
When I open the web page a captcha appears. It's not returning JSON at once.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
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'}
Reply
#8
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
Reply
#9
(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.
Reply
#10
also, don't use dict as variable name. it's built-in function
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Scapy, creating response for GET request. SyRex1013 4 5,121 Dec-11-2018, 07:39 AM
Last Post: SyRex1013

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020