fabriciocolombo / delphi-rest-client-api

A Delphi REST client API to consume REST services written in any programming language.
Apache License 2.0
381 stars 182 forks source link

MULTIPART/FORM-DATA exception #154

Open kidultttttt opened 9 months ago

kidultttttt commented 9 months ago

Hello! I'm trying to post an image. Unfortunately, the process doesn't work. The current routine is as follows. The problem is, the following exception is thrown: E2010 Incompatible types: 'string' and 'TObject' Shouldn't the post method receive such a parameter?

...
   TRequestFoto = class(TMultiPartFormData)
      vCaminhoFoto : TMultiPartFormAttachment;
   end;
...
function TuMercadoLivre.ValidaFoto(vCaminho, vToken : String): TJSONObject;
var
   vResponse : String;
   vRestClient : TRestClient;
   vRequestFoto : TRequestFoto;
begin
   vRequestFoto := TRequestFoto.Create;
   vRestClient := TRestClient.Create(Nil);
   Try
      Try
         vRestClient.ConnectionType := hctWinHttp;

         vRequestFoto.vCaminhoFoto := TMultiPartFormAttachment.Create('C:\SysFire\MercadoLivreFoto\000000000060_1.jpeg', 'multipart/form-data', '000000000060_1.jpeg');

         vResponse := vRestClient.
                      Resource('https://api.mercadolibre.com/pictures/items/upload').
                      Header('Authorization', 'Bearer ' + vToken).
                      ContentType('multipart/form-data').
                      Post(vRequestFoto);

         Result := TJSONObject(TJSONObject.ParseJSONValue(TEncoding.UTF8.GetBytes(vResponse), 0));
      Except
         On E : Exception Do
            Raise Exception.Create(E.Message);
      End;
   Finally
      vRestClient.Free;
      vRequestFoto.Free;
   End;
end;
maxkleiner commented 7 months ago

We use another component: https://github.com/maxkleiner/HttpComponent And multipart :files works

     `function TestHTTPClassComponentAPIDetection2(AURL, askstream, aApikey: string): string;
      var HttpReq1: THttpRequestC;
          Body: TMultipartFormBody;
          Body2: TUrlEncodedFormBody;
      begin
        Body:= TMultipartFormBody.Create;
        Body.ReleaseAfterSend:= True;
        //Body.Add('code','2','application/octet-stream');
        Body.AddFromFile('image', exepath+'randimage01.jpg');
        HttpReq1:= THttpRequestC.create(self);
        HttpReq1.headers.add('X-Api-Key:'+AAPIKEY);
        HttpReq1.headers.add('Accept:application/json');
        HttpReq1.SecurityOptions:= [soSsl3, soPct, soIgnoreCertCNInvalid];
        try
          if HttpReq1.Post1Multipart(AURL, body) then 
             writeln(HttpReq1.Response.ContentAsString)
          else Writeln('APIError '+inttostr(HttpReq1.Response.StatusCode2));
        finally  
          writeln('Status3: '+gethttpcod(HttpReq1.Response.statuscode2))
          HttpReq1.Free;  
          sleep(200)
          // if assigned(body) then body.free;
        end; 
      end;

`