I am trying to use requests module in python to communicate with a rest server
This is what I am trying to do:
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
data = {'foo':'foo','bar':'bar'}
r = requests.post("http://localhost:8080/foo",headers=headers,data=json.dumps(data))
print r.text
u'Resource representation is only available with these Content-Types:\napplication/json; charset=UTF-8'
How do I resolve this? Thanks
asked Apr 17, 2014 at 17:54
frazman
33.5k82 gold badges194 silver badges283 bronze badges
1 Answer 1
Try to change headers to:
headers = {'Accept': 'application/json; charset=UTF-8'}
upd: I think the most correct headers would be:
headers = {'Content-type': 'application/json', 'Accept': 'application/json'}
answered Apr 17, 2014 at 17:58
Bunyk
8,1458 gold badges52 silver badges85 bronze badges
Sign up to request clarification or add additional context in comments.
4 Comments
frazman
With your suggestion, I get this response "u'Resource representation is only available with these Content-Types:\ntext/plain; charset=UTF-8\ntext/plain"
frazman
I tried this headers = {'Content-type': 'application/json', 'Accept': 'text/plain; charset=UTF-8'} and it gave The server was not able to produce a timely response to your request
Bunyk
how with
headers = {'Accept': 'application/json'} or without headers?frazman
with this option.. i get u"There was a problem with the requests Content-Type:\nExpected 'application/json'"
lang-py
headers = {'Content-type': 'application/json; charset=UTF-8'}