0

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

1
  • Try this header: headers = {'Content-type': 'application/json; charset=UTF-8'} Commented Apr 17, 2014 at 18:12

1 Answer 1

2

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'}
Sign up to request clarification or add additional context in comments.

4 Comments

With your suggestion, I get this response "u'Resource representation is only available with these Content-Types:\ntext/plain; charset=UTF-8\ntext/plain"
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
how with headers = {'Accept': 'application/json'} or without headers?
with this option.. i get u"There was a problem with the requests Content-Type:\nExpected 'application/json'"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.