Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MULTIPART/FORM-DATA exception #154

Open
kidultttttt opened this issue Feb 26, 2024 · 1 comment
Open

MULTIPART/FORM-DATA exception #154

kidultttttt opened this issue Feb 26, 2024 · 1 comment

Comments

@kidultttttt
Copy link

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
Copy link

maxkleiner commented Apr 27, 2024

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;

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants