2

I have a delphi program calling a python rest service.

The python rest service looks like :

@app.route('/flagger/api/v1.0/stem', methods=['GET'])
def stem_request():
    print request.json
    if not request.json or u'text' not in request.json:
        bad_request('no "text" field given')
    text = request.json['text']
    return jsonify(stemmed=stem(text)), 200

When i do a curl request to that service i get the answer:

C:\Program Files\Curl>curl -i -H "Content-Type: application/json" -X GET -d "{\"text\":\"hij geeft mij appels\"}" h
    ttp://192.168.99.100:5000/flagger/api/v1.0/stem
    HTTP/1.0 200 OK
    Content-Type: application/json
    Content-Length: 30
    Server: Werkzeug/0.11.3 Python/2.7.10
    Date: Thu, 28 Jan 2016 13:17:36 GMT

    {
      "stemmed": "geeft appel"
    }

In my delphi XE7/Pro program i have a Restclient, Restrequest and Restresponse component.

All components have contentype=application/json

restrequest.method=rmGet

to the Restrequest i added a parameter with :

contenttype=ctapplication_json
kind=pkrequestbody 
name=text 
value=hij geeft mij appels.

The request is started with the following code:

MemoContent.Lines.Add('Start...');
RESTClient1.BaseURL:='http://192.168.99.100:5000/flagger/api/v1.0/stem';
MemoContent.Lines.Add('Request Params.Name= '+RESTRequest1.Params.Items[0].Name);
  MemoContent.Lines.Add('Request Params.Value= '+RESTRequest1.Params.Items[0].Value);
  RESTRequest1.Execute;
  MemoContent.Lines.Add('Status= '+RESTResponse1.StatusText);
  MemoContent.Lines.Add('Content= '+RESTResponse1.content);
  MemoContent.Lines.Add('FullRequestURI= '+RESTResponse1.FullRequestURI);
  MemoContent.Lines.Add('BaseURL= '+RESTClient1.BaseURL);
  jValue:=RESTResponse1.JSONValue;
  MemoContent.Lines.Add('Response Json= '+jValue.tostring);
  MemoContent.Lines.Add('Statustext= '+RESTResponse1.StatusText);

the request is executed but i always get a bad request answer:

Start...
Request Params.Name= text
Request Params.Value= dit is een test
Status= HTTP/1.0 400 BAD REQUEST
Content= {
  "error": "bad request"
}
FullRequestURI= http://192.168.99.100:5000/flagger/api/v1.0/stem
BaseURL= http://192.168.99.100:5000/flagger/api/v1.0/stem
Response Json= {"error":"bad request"}
Statustext= HTTP/1.0 400 BAD REQUEST

Anyone has an idea what's wrong here ?

I can't make it work with the rest debugger either but it works with CURL and other rest clients

Thanks.

lzs
  • 21
  • 3
  • You want to be sending a JSON document rather than HTTP parameters so you should probably create a JSON object and add that to the body of your request. http://docwiki.embarcadero.com/Libraries/XE7/en/REST.Client.TCustomRESTRequest.AddBody – Anthony Eischens Jan 28 '16 at 15:41
  • Also potentially of use: http://stackoverflow.com/questions/25022296/delphi-trestrequest-array-parameter/25047430#25047430 – Anthony Eischens Jan 28 '16 at 15:44
  • I already tried this and it gives the same problem str:='{"text":"hij geeft mij appels"}'; jobj:=tjsonobject.ParseJSONValue(tencoding.ASCII.GetBytes(str),0) as tjsonobject; MemoContent.Lines.Add('JSon request= '+jobj.tostring); restrequest1.ClearBody; restrequest1.AddBody(jobj); RESTRequest1.Execute; – lzs Jan 28 '16 at 16:08
  • This probably doesn't lead you to a solution but sending a body in a GET operation is frowned upon. http://stackoverflow.com/questions/978061/http-get-with-request-body I assume you have been looking into the output of the Python "print" statement that will show you what is received on the server side. One could suggest looking at the curl vs. Delphi operations with Wireshark but I think you have a lot of debugging options before you would have to go that route. – Anthony Eischens Jan 28 '16 at 18:57
  • 1
    After some testing here in Delphi Seattle it appears the REST framework is not sending the GET body. When I flip to POST the body is sent to the server. So you might find some benefit in flipping to POST OR adding your JSON as a parameter on the GET request (i.e. http://192.168.99.100:5000/flagger/api/v1.0/stem?request={"text":"hij geeft mij appels"} (all properly encoded of course). – Anthony Eischens Jan 28 '16 at 19:26

0 Answers0