RTC Forums
June 03, 2020, 09:29:15 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Login Register  
Pages: [1]
  Print  
Author Topic: File Upload  (Read 492 times)
manoi
RTC License
***
Posts: 8


« 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 ?


Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1814


« Reply #1 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
Logged
Star5
RTC License+
****
Posts: 26


« Reply #2 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;
Logged
manoi
RTC License
***
Posts: 8


« Reply #3 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 ?
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1814


« Reply #4 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
Logged
manoi
RTC License
***
Posts: 8


« Reply #5 on: January 29, 2020, 09:07:48 AM »

Thank you.
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.026 seconds with 17 queries.