ogalonzo 2 Posted September 12, 2023 (edited) Hi: I've been trying to use ICS to implement communication with a biometric device that has an API. I'm stuck as of now trying to send a POST request with a JSON body, the biometric device keeps answering with a HTTP 400 (Bad request) and can't find out why. So I want to discard issues, starting with my code, which BTW I don't really know if it is wrong or right. It goes as follows: procedure TForm1.cmdAddUserClick(Sender: TObject); Var astrJSON : TStringList; nStatCode : Integer; fs : TStringStream; begin astrJSON:=TStringList.Create(); astrJSON.LoadFromFile('hk-new-user.json'); // JSON is well formed, that's not the issue, Note: path edited fs:=TStringStream.Create(); With dmHK.SslHttpRest1 Do begin ContentTypePost := 'application/json'; RestParams.Clear(); RestParams.AddItem('raw', astrJSON.Text); RestParams.PContent := PContBodyJson; RcvdStream:=fs; nStatCode := RestRequest(httpPOST, 'https://xxx.xxx.xxx.xxx/ISAPI/AccessControl/UserInfo/Record?format=json', False, ''); // IP edited fs.WriteString(sLineBreak + 'Headers: ' + sLineBreak + RcvdHeader.Text + sLineBreak + 'Status code: ' + IntToStr(StatusCode) + sLineBreak + 'nStatusCode: ' + IntToStr(nStatCode)); fs.Position:=0; Memo1.Lines.Clear(); Memo1.Lines.LoadFromStream(fs, TEncoding.UTF8); end; fs.Free(); astrJSON.Free(); end; Other requests work fine (they're GETs BTW). I don't know much of ICS, sorry if I'm asking a dumb question, but I can't find where is the error. Any ideas? Edited September 12, 2023 by ogalonzo Share this post Link to post
Fr0sT.Brutal 900 Posted September 13, 2023 I'd try to execute the request with cURL and if it works, compare the contents of both requests Share this post Link to post
Angus Robertson 577 Posted September 13, 2023 Quote RestParams.AddItem('raw', astrJSON.Text); Are you intending to nest Json here, using a parameter like this will create a new Json item named raw with your Json as the value. Except when adding raw parameters like Json, you need a third parameter True so that the item is not escape encoded. If your Json is complete, you can ignore RestParams and just add it as RawParameters which is the last argument in RestRequest. The component has built in logging, set DebugLevel to DebugBody, assign the onHttpRestProg event, and write everything that arrives to your memo, Angus Share this post Link to post
ogalonzo 2 Posted September 13, 2023 4 hours ago, Angus Robertson said: Are you intending to nest Json here, using a parameter like this will create a new Json item named raw with your Json as the value. Except when adding raw parameters like Json, you need a third parameter True so that the item is not escape encoded. If your Json is complete, you can ignore RestParams and just add it as RawParameters which is the last argument in RestRequest. The component has built in logging, set DebugLevel to DebugBody, assign the onHttpRestProg event, and write everything that arrives to your memo, Angus As a matter of fact I don't know what I'm doing 😞 (like I said I don't know much of ICS); what I'm *trying* to do is to make that JSON to be sent as body, which I still don't know how to achieve. Is there a property or method for that? thanks in advance. Share this post Link to post
Angus Robertson 577 Posted September 13, 2023 I explained how in the third line: nStatCode := RestRequest(httpPOST, 'https://xx', False, astrJSON.Text); You can also look at the new OverbyteIcsSnippets sample in ICS v9 which has working examples of many ICS functions, just click a button and watch it happen, although you are almost there already. But do use the debugs options, you'd then have seen the modified Json being sent, and the problem should have been glaringly obvious. Angus 1 Share this post Link to post
ogalonzo 2 Posted September 13, 2023 Just now, Angus Robertson said: I explained how in the third line: nStatCode := RestRequest(httpPOST, 'https://xx', False, astrJSON.Text); You can also look at the new OverbyteIcsSnippets sample in ICS v9 which has working examples of many ICS functions, just click a button and watch it happen, although you are almost there already. But do use the debugs options, you'd then have seen the modified Json being sent, and the problem should have been glaringly obvious. Angus Thank you very much, I'll try it. Share this post Link to post
ogalonzo 2 Posted September 13, 2023 1 hour ago, Angus Robertson said: I explained how in the third line: nStatCode := RestRequest(httpPOST, 'https://xx', False, astrJSON.Text); You can also look at the new OverbyteIcsSnippets sample in ICS v9 which has working examples of many ICS functions, just click a button and watch it happen, although you are almost there already. But do use the debugs options, you'd then have seen the modified Json being sent, and the problem should have been glaringly obvious. Angus It worked just fine. Thank you very much! 1 Share this post Link to post