ogalonzo
Members-
Content Count
10 -
Joined
-
Last visited
Community Reputation
2 NeutralRecent Profile Visitors
The recent visitors block is disabled and is not being shown to other users.
-
TSslHttpRest and multipart/form-data
ogalonzo replied to ogalonzo's topic in ICS - Internet Component Suite
Finally managed to solve it. I was wrong about the image, I had to send it but in binary form. Final code is this, unnecesary parts removed: {msCont = TMemoryStream bwCont = TBinaryWriter fs = TFileStream} s:='capture.jpg'; strMIMEBoundary:=IntToHex(Random(MaxInt), 8); { Note: my device requires digest authentication, change or delete next three lines if yours does not } HttpCli1.ServerAuth:=httpAuthDigest; HttpCli1.Username:='xxxx'; HttpCli1.Password:='xxxx'; HttpCli1.ExtraHeaders.Add('Connection=keep-alive'); HttpCli1.ContentTypePost:= 'multipart/form-data; boundary=' + strMIMEBoundary; strMIMEBoundary:='--' + strMIMEBoundary; slBuf.LoadFromFile(ExtractFilePath(Application.ExeName) + 'data.json', TEncoding.UTF8); msCont:=TMemoryStream.Create(); bwCont:=TBinaryWriter.Create(msCont); bwCont.Write(TEncoding.UTF8.GetBytes(strMIMEBoundary + sLineBreak + 'Content-Disposition: form-data; name="data"' + sLinebreak + sLinebreak)); bwCont.Write(TEncoding.UTF8.GetBytes(slBuf.Text)); bwCont.Write(TEncoding.UTF8.GetBytes(strMIMEBoundary + sLineBreak)); bwCont.Write(TEncoding.UTF8.GetBytes('Content-Disposition: form-data; name="image"; filename="capture.jpg"' + sLineBreak)); bwCont.Write(TEncoding.UTF8.GetBytes('Content-Type: image/jpeg' + sLineBreak + sLinebreak)); { Create stream from image } fs:=TFileStream.Create(s, fmOpenRead); fs.Position:=0; { Dump entire image to content } msCont.CopyFrom(fs, fs.Size); fs.Free(); { Write MIME end marker } bwCont.Write(TEncoding.UTF8.GetBytes(sLineBreak + strMIMEBoundary + '--' + sLineBreak)); bwCont.Free(); ss:=TStringStream.Create(); msCont.Position:=0; HttpCli1.SendStream:=msCont; HttpCli1.SendStream.Position:=0; HttpCli1.RcvdStream:=ss; ss.Position:=0; HttpCli1.URL:='http://' + STR_HOST + '/ISAPI/Intelligent/FDLib/FaceDataRecord?format=json'; HttpCli1.Post(); ss.Free() end; End Thanks to everyone for your time. -
TSslHttpRest and multipart/form-data
ogalonzo replied to ogalonzo's topic in ICS - Internet Component Suite
I changed to TSslHttpCli, and coded this: // Note: current process I'm implementing comprises two steps: first one is sending a request to take a photo on the biometric device, then the // device answers with the photo embedded in the request. From here it starts the following code, which tries to emulate more or less what I saw // in your example: if (d.PartCount > 0) then Begin f:=d.Part(0); { Found one part, check type } if Assigned(f) and (f.ContentType = 'image/jpeg') then begin s:=ExtractFilePath(Application.ExeName) + 'capture.jpg'; f.SaveToFile(s); strBdry := '-----------------------------' + IntToHex(Random(MaxInt), 8) + IntToHex(Random(MaxInt), 8); SslHttpCli1.ContentTypePost:= 'multipart/form-data; boundary=' + strBdry; SslHttpCli1.Connection := 'Keep-Alive'; MimeHeader:=strBdry + sLineBreak + 'Content-Disposition: form-data; name="data"' + sLinebreak + '{' + slineBreak + ' "faceLibType": "blackFD",' + slineBreak + ' "FDID": "1",' + slineBreak + ' "FPID": "2000",' + slineBreak + ' "featurePointType":"face"' + slineBreak + '}' + slineBreak + strBdry + sLineBreak + 'Content-Disposition: form-data; name="image" filename="capture.jpg"' + sLineBreak; MimeFooter:=strBdry + '--' + sLineBreak; SslHttpCli1.SendStream:=TMultiPartFileReader.Create (s, MimeHeader, MimeFooter); SslHttpCli1.SendStream.Position:=0; SslHttpCli1.URL:='https://' + STR_HOST + '/ISAPI/Intelligent/FDLib/FaceDataRecord?format=json'; SslHttpCli1.Post(); end; End; d.Free(); end; However I get an AV on SslHttpCli1.Post();. Any idea what I'm doing wrong? -
TSslHttpRest and multipart/form-data
ogalonzo replied to ogalonzo's topic in ICS - Internet Component Suite
Ok, tried again and failed miserably. I'll try to describe what I'm using in Postman, and if you can point me to what to use in ICS that would be more than enough: In Postman, Body, then form-data I see two rows: 1) Key=data, Value=JSON 2) Key=image, value=<filename> (you're right, is just the filename) Do I define those two as parameters in TSslHttpRest? Additionally (from what I understood from the example you mentioned earlier) I have to generate by hand the boundary part, is that correct? Thanks in advance. -
TSslHttpRest and multipart/form-data
ogalonzo replied to ogalonzo's topic in ICS - Internet Component Suite
Well, I tried and no luck. I mean I tried using just the example (OverbyteIcsHttpRestTst), and it didn't work, Surely is something that I'm doing wrong. Also I re-read my original question and I didn't a good job describing my problem: I'm trying to implement an API from a biometric (face, finger and card) control device. I'm trying to register a face, which requires to send a request to {{host}}/ISAPI/Intelligent/FDLib/FaceDataRecord?format=json Content is multipart/form-data. Body has to specify two parts, one named "data" which contains a JSON, another one named "image" which contains full local path to the image to be sent for biometric processing. I tried using the REST parameters for this, couldn't make it work, always received status 400 (bad request). I'm in the dark here. Cloud you please point me in the right direction? whatever help you can give me is greatly appreciated. Thanks in advance. -
TSslHttpRest and multipart/form-data
ogalonzo replied to ogalonzo's topic in ICS - Internet Component Suite
Ok I'll try it. Thank you very much! -
Is there an example of how to send an image with TSslHttpRest with content as multipart/form-data? I'm a little confused reading closest example that I could find (with THttpCli BTW). Thanks in advance.
-
ICS with POST request and JSON body
ogalonzo replied to ogalonzo's topic in ICS - Internet Component Suite
It worked just fine. Thank you very much! -
ICS with POST request and JSON body
ogalonzo replied to ogalonzo's topic in ICS - Internet Component Suite
Thank you very much, I'll try it. -
ICS with POST request and JSON body
ogalonzo replied to ogalonzo's topic in ICS - Internet Component Suite
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. -
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?