Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: tidhttp with params


This question is answered.


Permlink Replies: 2 - Last Post: Nov 8, 2017 10:21 PM Last Post By: Fuandi Hartanto
Fuandi Hartanto

Posts: 18
Registered: 1/12/15
tidhttp with params  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 8, 2017 2:14 AM
Need help with tidhttp. I have a requirement to send a http post with these parameter
Parameters : file
Parameter type: formdata
Note: file to be uploaded

Parameters : data
Parameter type: formdata
Note: json parameter

When I run these codes below, seems got invalid response from the server, did I do something wrong ?

Params := TIdMultipartFormDataStream.Create;

try
with IdHTTP7 do
begin
Request.CustomHeaders.Add('Accept: application/json');
Request.CustomHeaders.Add('Content-Type: multipart/form-data');
Params.addfile('file','C:\Hello.doc');
Body_data:='{"Name": "Michael","documentTypeId": "1"}';

Params.AddFormField('data',Body_data);

try
Response.KeepAlive := False;
Post('http://testing.com/upload', Params, aStream);

except
on E: Exception do
showmessage('Error encountered during UPLOAD: ' + E.Message);
end;

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: tidhttp with params
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 8, 2017 1:54 PM   in response to: Fuandi Hartanto in response to: Fuandi Hartanto
Fuandi Hartanto wrote:

When I run these codes below, seems got invalid response from the
server, did I do something wrong ?

Yes. DO NOT set the 'Content-Type' header using the
Request.CustomHeaders property. Use the Request.ContentType property
instead.

However, the TIdMultipartFormDataStream overload of TIdHTTP.Post()
already sets that property for you, since TIdMultipartFormDataStream
dynamically generates a MIME boundary that must be included in the
'Content-Type' header. So, you shouldn't be setting the 'Content-Type'
header manually at all when posting a TIdMultipartFormDataStream.

By using CustomHeaders property the way you are, you are actually
causing TIdHTTP to send 2 'Content-Type' headers, and a conforming
server would concat their values together, producing a bad
'Content-Type' value.

--
Remy Lebeau (TeamB)
Fuandi Hartanto

Posts: 4
Registered: 7/16/14
Re: tidhttp with params  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 8, 2017 10:21 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Hi Remy, thanks for the suggestion. My first time using idhttp. One more question. I need to send params with type application/json.
So I use this code below, is this correct ?

Params.AddFormField('data',Body_data,'utf-8','application/json');

Thanks before
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02