Title: File Upload Post by: manoi on January 28, 2020, 10:09:46 AM I need to send data to webservice. They give me the following example :
curl -X POST \ https://www.gw.com/api/v1/service/x1/ \ -H 'Authorization: Bearer abcdefg' \ -H 'Host: www.gw.com' \ -F 'file=@{PATH_TO_IMAGE};type=application/x-image' \ -F id={Service ID};type=application/json I use example from rtcClientFormPost, Do you have any suggestion how to modify example to send data with file and json ? Title: Re: File Upload Post by: D.Tkalcec (RTC) on January 28, 2020, 10:36:17 AM Sorry, but the "Request.Params" property (TRtcHttpValues class) ONLY provides the functionality required for posting simple form variables to Web Servers. It does NOT provide the functionality required for uploading files. If you need to send anything else than simple text fields, you will have to manually construct request headers and content body.
Best Regards, Danijel Tkalcec Title: Re: File Upload Post by: Star5 on January 28, 2020, 01:41:58 PM vHttp := TNetHTTPClient.Create(nil);
vS := TStringStream.Create('', TEncoding.GetEncoding(65001)); try vS.Clear; with vHttp do begin ConnectionTimeout := 2000; ResponseTimeout := 10000; AcceptCharSet := 'utf-8'; AcceptLanguage := 'en-US'; ContentType := 'application/json'; // UserAgent := 'Embarcadero URI Client/1.0'; CustomHeaders['Authorization'] := 'Bearer ' + access_token; CustomHeaders['X-EBAY-C-MARKETPLACE-ID'] := 'EBAY_US'; try Get('https://api.ebay.com/sell/fulfillment/v1/order?' // + 'orderIds=' + vOrderIds + 'filter=' + TNetEncoding.URL.Encode(vFilter) + '&limit=' + vLimit + '&offset=' + vOffset, vS); mmo2.Lines.Text := TNetEncoding.URL.URLDecode(vS.DataString); except on E: Exception do mmo2.Lines.Text := 'error:' + E.Message; end; end; finally vS.Free; vHttp.Free; end; File, you can see if the interface supports formdata. procedure TfrmMain.Button1Click(Sender: TObject); var cHttp: TNetHTTPClient; vData: TMultipartFormData; vRsp: TStringStream; i: Integer; begin if OpenDialog1.Execute then begin cHttp := TNetHTTPClient.Create(nil); vRsp := TStringStream.Create('', TEncoding.GetEncoding(65001)); try for i := 0 to OpenDialog1.Files.Count - 1 do begin vData := TMultipartFormData.Create; try vData.AddFile('fname', OpenDialog1.Files.Strings); vData.AddField('utt', LabeledEdit1.Text); with cHttp do begin ConnectionTimeout := 2000; ResponseTimeout := 10000; AcceptCharSet := 'utf-8'; AcceptEncoding := '65001'; AcceptLanguage := 'zh-CN'; ContentType := 'multipart/form-data'; UserAgent := 'Embarcadero URI Client/1.0'; try Memo1.Lines.Add('upload file : ' + OpenDialog1.Files.Strings); Post('http://localhost:8833/upfile', vData, vRsp); Application.ProcessMessages; except on E: Exception do Memo1.Lines.Add('Error:' + E.Message); end; Memo1.Lines.Add(vRsp.DataString); end; finally vData.Free; end; end; finally cHttp.Free; vRsp.Free; end; end; end; Title: Re: File Upload Post by: manoi on January 29, 2020, 03:11:46 AM Sorry, but the "Request.Params" property (TRtcHttpValues class) ONLY provides the functionality required for posting simple form variables to Web Servers. It does NOT provide the functionality required for uploading files. If you need to send anything else than simple text fields, you will have to manually construct request headers and content body. Best Regards, Danijel Tkalcec Is that possible with RTC ? Can you provide an example code ? Title: Re: File Upload Post by: D.Tkalcec (RTC) on January 29, 2020, 08:41:17 AM Since RTC does NOT have any classes or functions for constructing the content body of a multipart-form-data request containing files (only simple text fields), I can NOT provide an example for uploading a file using the multipart-form-data format with RTC.
Best Regards, Danijel Tkalcec Title: Re: File Upload Post by: manoi on January 29, 2020, 09:07:48 AM Thank you.
|