2

I'm trying to do the following from python:

This work:

curl -i -H "Content-Type: application/json" -H "Accept: application/json" -X GET  -d "{\"Dato\":\"1\"}" http://localhost:5000/api/v1/recurso -H " Authorization: JWT ...p_GWs2XOAY" 

My resource:

class RecursoPrivado(Resource):

@jwt_required()
def get(self):
    json_data = request.get_json(force=True)
    #data  = json.loads(json_data)
    return json_data

api.add_resource(RecursoPrivado, '/recurso')

I tried this, but return Response [401]

url = 'http://localhost:5000/api/v1/recurso'
data={"Dato":"1"}
token="...p_GWs2XOAY" 
response=requests.get(url, data=data, headers={'Authorization':'JWT '+token})

Any ideas?

7
  • Try json=data instead. Right now you're sending form-encoded data. Commented Jul 28, 2017 at 0:38
  • As an aside, it's somewhat non-standard to include a body with a GET request. Consider using a POST request instead. Commented Jul 28, 2017 at 0:39
  • That code should pretty much work, but you need to change requests.post to requests.get. (I imagine that tool got it wrong because it's non-standard to include a request body with a GET request.) But did you try my suggestion? That seems simpler. Commented Jul 28, 2017 at 1:00
  • For those who are confused, my previous comment was in response to a comment that has since been deleted. :-) Commented Jul 28, 2017 at 1:00
  • I regret to have deleted it, I published it more clearly Commented Jul 28, 2017 at 1:08

1 Answer 1

2
response = requests.get(url, data=data, headers={'Authorization': 'JWT '+token})

should be this:

response = requests.get(url, json=data, headers={'Authorization': 'JWT '+token})

You're currently sending form-encoded data rather than JSON-encoded data.

Sign up to request clarification or add additional context in comments.

Comments

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.