Jump to content
Joe Sansalone

trying to POST multiple values of same parameter

Recommended Posts

I'm trying to add the same parameter name multiple times with different values (using POST).

It seems like separating values via semi-colon ; and using poFlatArray only works for a GET request.

 

FRestRequest.Params.AddItem('MediaUrl', values , TRestRequestParameterKind.pkGETorPOST, [poFlatArray]);

 

Simply doing multiple AddItem erases previous entry if the parameter name is the same.

So, How do I add the same parameter multiple times?  

 

 

Edited by Joe Sansalone

Share this post


Link to post
Quote

And how do you think the server would address those parameters?

The server can enumerate the parameters sequentially, rather than by accessing them by name, I just added a function to ICS to do exactly that, for diagnostic purposes.

 

But comma separated values could be used as an array.  It all depends on what the server expects, and it may not follow normal standards. 

 

Angus

 

Share this post


Link to post

The server is expecting same name parameter multiple times.  

But the TRestRequest.Params don't support adding the same parameter name again.

Share this post


Link to post

Have you tried using the parameterless AddItem function overload and set the Name, Value and Kind in its result?

Share this post


Link to post
2 hours ago, Uwe Raabe said:

Have you tried using the parameterless AddItem function overload and set the Name, Value and Kind in its result?

It searches for the param with the name, and changes the value (and other properties of the parameter)

I don't think this is possible with the default parameters in the TRestRequest.

 

You have to fetch the TRestRequest parameter and use  the .AddValue method  i think.

 

 

Edited by mvanrijnen

Share this post


Link to post
34 minutes ago, mvanrijnen said:

It searches for the param with the name, and changes the value (and other properties of the parameter)

It doesn't even get a name to search for:

function TRESTRequestParameterList.AddItem: TRESTRequestParameter;
begin
  Result := Add as TRESTRequestParameter;
end;

 

Share this post


Link to post
14 minutes ago, Uwe Raabe said:

It doesn't even get a name to search for:


function TRESTRequestParameterList.AddItem: TRESTRequestParameter;
begin
  Result := Add as TRESTRequestParameter;
end;

 

Which Delphi version? 

In 11.3 there are a couple of overloads, but not that "overload version"?

found it 🙂  overread the parameterless in your post 

 

that in combination with the .AddValue would be the solution 

Edited by mvanrijnen

Share this post


Link to post

I tried and it's not working.  

Is there a way to output the HTTP request text?  The actual text that is being sent for the whole request?

 

 

 

 

  • Like 1

Share this post


Link to post

Best way is to use a proxy, for a list see for example: mitmproxy Alternatives: Top 10 HTTP(S) & Web Debuggers | AlternativeTo

( i use mitm and/or fiddler, charles is also mentioned by @Uwe Raabe i believe)
 

let mee see if i can capture some output for this.

Here it works like a charm.....

 

var
......
prm : TRESTRequestParameter;
begin
.....
  req := TRESTRequest.Create(nil);
  try
    req.Client := FRestClient;
    req.Method := TRESTRequestMethod.rmPost;
    req.Resource := cExtensionResource;
.....
    prm := req.Params.AddItem();
    prm.Name := 'testmultivalue';
    prm.AddValue('Value_1');
    prm.AddValue('Value_2');
    prm.AddValue('Value_3');
    prm.AddValue('Value_A');

    req.Execute();
.....

 

don't mind the 400 error, i just changed a get into a post to try.

 

 

image.thumb.png.d919f7efcea1b2db21bd8b8577fff57c.png

 

 

Edited by mvanrijnen

Share this post


Link to post

Don ;t think thats possible with the Params property,  it all ends up merging the diffentt params from client/request etc, and there they get overwritten when having the same name.

You have to fill your request body by hand then i think.

 

Share this post


Link to post
21 minutes ago, mvanrijnen said:

You have to fill your request body by hand then i think.

I guess the problem that these parameters are in the URL query.

Share this post


Link to post

OK, I got it to work via:

 

tempBody := 'Param1=' + value1 + '&' + 'Param2=' + value2 + '&' ... etc;  // build the parameters with & in-between each parameter

 

RestRequest.Params.AddItem('Body1234', tempBody, pkREQUESTBODY, [poDoNotEncode]);

 

// notice that there needs to be a unique name even for the entire body of parameters.  

 

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×