Search code examples
filedelphipostindy

Delphi Indy POST file without "fieldname" in TIdMultiPartFormDataStream


Using Delphi 10.1. Need to send image to this site: www.flagma.ru

At sniffer requests:

URL:https://flagma.ru/messageuploadphoto.php?qqfile=111.jpg
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3
Content-Type: application/octet-stream
Referer: https://flagma.ru/my/1538481/podat-obyavlenie.html
X-File-Name:111.jpg
X-Mime-Type: image/jpeg
X-Requested-With: XMLHttpRequest

My Delphi trying:

http.Request.ContentType := 'application/octet-stream';
  http.Request.Accept := '*/*';
  http.Request.AcceptLanguage := 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7';
  http.Request.AcceptEncoding := 'gzip, deflate';
  http.Request.Referer := 'https://flagma.ru/my/1538481/podat-obyavlenie.html';

  fnShort := ExtractFileName(fn); // fn - full image file
  url := 'https://flagma.ru/messageuploadphoto.php?qqfile=' + fnShort;
  http.Request.CustomHeaders.AddValue('X-Mime-Type', 'image/jpeg');
  http.Request.CustomHeaders.AddValue('X-File-Name', fnShort);
  http.Request.CustomHeaders.AddValue('X-Requested-With', 'XMLHttpRequest');

  (* it is wrong way
  a := TStringStream.Create(tempa);
  formData := TIdMultiPartFormDataStream.Create;
  formData.Clear; 
  formData.AddFile('qqfile', fn, GetMIMETypeFromFile(fn));

  try
    http.Post(url, formData, a);
  except
    on E: EIdException do
    begin
      response := '';
    end;
  end;
  formData.Free;
  response := utf8Decode(a.DataString);
 *)

Solution is just: response := http.Post(url, fn);

And getting error: Imagу has small width and height. But in browser same file is ok.


Solution

  • Your server is expecting the file to be posted as-is in the POST body. As such, you need to use the overloaded version of TIdHTTP.Post() that takes a plain TStream, such as a TFileStream, instead of using the overload that takes a TIdMultipartFormDataStream:

    http.Request.ContentType := 'application/octet-stream';
    http.Request.Accept := '*/*';
    http.Request.AcceptLanguage := 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7';
    //http.Request.AcceptEncoding := 'gzip, deflate';
    http.Request.Referer := 'https://flagma.ru/my/1538481/podat-obyavlenie.html';
    
    fnShort := ExtractFileName(fn); // fn - full image file
    url := 'https://flagma.ru/messageuploadphoto.php?qqfile=' + fnShort;
    http.Request.CustomHeaders.Values['X-Mime-Type'] := GetMIMETypeFromFile(fn);
    http.Request.CustomHeaders.Values['X-File-Name'] := fnShort;
    http.Request.CustomHeaders.Values['X-Requested-With'] := 'XMLHttpRequest';
    
    fs := TFileStream.Create(fn, fmOpenRead or fmShareDenyWrite);
    try
      try
        response := http.Post(url, fs);
      except
        response := '';
      end;
    finally
      fs.Free;
    end;
    
    { alternatively: you can pass the full file path to Post()...
    try
      response := http.Post(url, fn);
    except
      response := '';
    end;}