0

I do some test with a REST API.

When I do a POST call :

Param := TMultipartFormData.Create;

Param.AddField('param1', 'value1');
Param.AddField('param2', 'value2');

Response := Client.Post('http://localhost/project/public/login', Param);

I can use param1 and param2 in my backend.

When I do same with PUT method, I can't use my param, param1 and param2 are null

Param := TMultipartFormData.Create;

Param.AddField('param1', 'value1');
Param.AddField('param2', 'value2');

// Headers contain my JWT

Response := Client.Put('http://localhost/project/public/api/profile', Param, nil, Headers); 

To use my param in PUT call I need to add it in my URL but I didn't want use it like that.

Client.Put(
   'http://localhost/project/public/api/profile?param1=value1', 
   nil, nil, Headers); // work but bad solution

I use Firemonkey 10.4.2 .

EDIT: My PUT call work in Postam when I'm with body x-www-form-urlencoded. When I use form-data, I get null param.

How can I give param in x-www-form-urlencoded ?

Bosshoss
  • 783
  • 6
  • 24
  • This is entirely dependent on how the webserver implements its `PUT` handler. If it requires params to be passed in the URL's query string instead of in the HTTP message body, there is nothing you can do about that on the client side. `multipart/form-data` is not a common data type used in `PUT` requests, but is very common in `POST` requests. – Remy Lebeau Jan 05 '22 at 19:56
  • What I didn't understand is that for both, it's work in my Postman.. – Bosshoss Jan 05 '22 at 20:04
  • 2
    Then you should include those details in your question. It would also help if you showed your server code, too. – Remy Lebeau Jan 05 '22 at 20:05
  • Ok it work in x-www-form-urlencoded, not in form-data. How can I use this type of param with `THTTPClient` ? – Bosshoss Jan 05 '22 at 20:12
  • 1
    [`THTTPClient.Post()`](https://docwiki.embarcadero.com/Libraries/en/System.Net.HttpClient.THTTPClient.Post) and [`THTTPClient.Put()`](https://docwiki.embarcadero.com/Libraries/en/System.Net.HttpClient.THTTPClient.Put) both have overloads that take a `TStrings` as input. Try passing it a `TStringList` containing `name=value` strings, eg: `Param := TStringList.Create; Param.Add('param1=value1'); Param.Add('param2=value2'); Response := Client.Put('http://localhost/project/public/api/profile', Param, nil, TEncoding.UTF8, Header);` – Remy Lebeau Jan 05 '22 at 20:20
  • 1
    Well, there's a reason why it is named `TMultipartFormData` and always performs `multipart/form-data`. You must then not use that class if you want something different. And since `application/x-www-form-urlencoded` is a simple format of `param1=one&param2=two&param3=three` it is obvious to use `TStrings`, having each pair per line. [See also](https://stackoverflow.com/a/4073451/4299358). – AmigoJack Jan 12 '22 at 01:43

0 Answers0