python - Calling a Flask REST service method in different OS with curl -
i wrote following post method rest api, built using flask. method receives 1 parameter, radio station url.
@app.route('/todo/api/v1.0/predvajaj', methods=['post']) def create_task(): print "expression value: " + str(not request.json or not 'title' in request.json) if not request.json or not 'title' in request.json: abort(400) link=request.json['title'] print "link value: " + link cmd = "pkill sox" os.system(cmd) time.sleep(2) #link = "http://www.radiostationurl.m3u" cmd = "sox -t mp3 " + link + " -t wav -r 22050 -c 1 - | sudo ../pifm - 90.5 &" os.system(cmd) return jsonify({'status': "ok"}), 201
the api runs on raspberry pi ip address: 192.168.0.200. tried testing method locally (on pi), using curl tool. worked fine:
curl -i -h "content-type: application/json" -x post -d '{"title":"http://www.radiostationurl.m3u"}' http://192.168.0.200:5000/todo/api/v1.0/predvajaj
then tried testing testing computer (running windows) in same lan with same command , tool, following error:
http/1.0 400 bad request content-type: text/html content-length: 192 server: werkzeug/0.10.4 python/2.7.3 date: wed, 05 aug 2015 11:06:05 gmt <!doctype html public "-//w3c//dtd html 3.2 final//en"> <title>400 bad request</title> <h1>bad request</h1> <p>the browser (or proxy) sent request server not understand.</p>
webserver output (two requests - 1 pi , other pc):
expression value: false 192.168.0.200 - - [05/aug/2015 11:05:56] "post /todo/api/v1.0/predvajaj http/1.1" 201 - sox warn wav: length in output .wav header wrong since can't seek fix 192.168.0.103 - - [05/aug/2015 11:06:05] "post /todo/api/v1.0/predvajaj http/1.1" 400
so problem in evaluation of if expression. can tell me why failing evaluate?
edit: tried running curl -v
switch @meuh suggested. content-length different.
pi:
* connect() 192.168.0.200 port 5000 (#0) * trying 192.168.0.200... * connected * connected 192.168.0.200 (192.168.0.200) port 5000 (#0) > post /todo/api/v1.0/predvajaj http/1.1 > user-agent: curl/7.26.0 > host: 192.168.0.200:5000 > accept: */* > content-type: application/json > content-length: 51 > * upload sent off: 51 out of 51 bytes * additional stuff not fine transfer.c:1037: 0 0 * additional stuff not fine transfer.c:1037: 0 0 * additional stuff not fine transfer.c:1037: 0 0 * http 1.0, assume close after body < http/1.0 201 created < content-type: application/json < content-length: 27 < server: werkzeug/0.10.4 python/2.7.3 < date: wed, 05 aug 2015 13:49:01 gmt < { "status": "predvajam" * closing connection #0 }
windows:
* connect() 192.168.0.200 port 5000 (#0) * trying 192.168.0.200... * connected 192.168.0.200 (192.168.0.200) port 5000 (#0) > post /todo/api/v1.0/predvajaj http/1.1 > host: 192.168.0.200:5000 > user-agent: curl/7.43.0 > accept: */* > content-type: application/json > content-length: 49 > * upload sent off: 49 out of 49 bytes * http 1.0, assume close after body < http/1.0 400 bad request < content-type: text/html < content-length: 192 < server: werkzeug/0.10.4 python/2.7.3 < date: wed, 05 aug 2015 13:50:51 gmt < <!doctype html public "-//w3c//dtd html 3.2 final//en"> <title>400 bad request</title> <h1>bad request</h1> <p>the browser (or proxy) sent request server not understand.</p> * closing connection 0
i overlooked note issue, on site tutorial, says:
note: if on windows , use cygwin version of curl bash above command work fine. however, if using native version of curl regular command prompt there little dance needs done send double quotes inside body of request. on windows have use double quotes enclose body of request, , inside escape double quote writing 3 of them in sequence.
the correct command, in case, is:
curl -i -h "content-type: application/json" -x post -d "{"""title""":"""http://www.radiostationurl.m3u"""}" http://192.168.0.200:5000/todo/api/v1.0/predvajaj
Comments
Post a Comment