Page 1 of 1

TScHttpWebRequest vs multipart data

Posted: Wed 22 Apr 2020 16:32
by DmitriNL
Hi,

Is there a clear example how to implement multipart data using TScHttpWebRequest? I found a topic but the solution does not seem to work. viewtopic.php?f=27&t=39705

I tried the TIdMultiPartFormDataStream from the Indy components to create multipart data. The multipart data is send by TScHttpWebRequest. The server does not seem to understand the result.

Code: Select all

	String FileName = "myFile.pdf";
	String URL = "https://my-url.com/";

	TScHttpWebRequest *Request = new TScHttpWebRequest(URL);
	TIdMultiPartFormDataStream *DataStream = new TIdMultiPartFormDataStream();
	try
	{
		DataStream->AddFormField("field1", "1234");
		DataStream->AddFile("file1", FileName, "application/pdf");
		DataStream->Position = 0;

		Request->Method = rmPOST;
		Request->ContentType = "multipart/form-data";
		Request->ContentLength = DataStream->Size;
		Request->SendChunked = true;
		Request->RequestStream = DataStream;

		TScHttpWebResponse *Response = Request->GetResponse();
		try
		{
			Memo1->Text = Response->ReadAsString();
		}
		__finally
		{
			Response->Free();
		}
	}
	__finally
	{
		DataStream->Free();
		Request->Free();
	}
Probably I am doing something wrong. Can you please help me out? Thank you in advance for your response.

Re: TScHttpWebRequest vs multipart data

Posted: Fri 24 Apr 2020 10:01
by ViktorV
Your code is correct.
To give you a detailed answer, we need a complete example demonstrating the incorrect behavior.