1

I am trying to send an Object as part of httprequest. The values are populated from Specflow table.

public class Request
{
    public Dictionary<string, dynamic> RequestParameters = 
                                                    new Dictionary<string, dynamic>();
    public void setRequestParameters(Table table)
    {

        foreach (var row in table.Rows)
        {
            try
            {
                RequestParameters.Add(row[0], row[1]);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}

Request request = new Request();
request.setRequestParameters(table);
var result = client.PostAsJsonAsync<Request>(_address.ToString(), request).Result;

The value is sent however I don't want the memberName(RequestParameters) enclosing the values. Is there a way to ignore it?

{
   "RequestParameters":{  
      "InitialCashAmount":"10000.00",
      "TransferAmount":"5000.00",
      "PersonalRegularContribution":"100.00"
   }
}
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
DesertRose
  • 23
  • 1
  • 7
  • What if you pass request.RequestParameters instead of request itself to PostAsJsonAsync? Do you see any difference? – Siva Gopal Sep 29 '15 at 12:53
  • Any reason you're using `dynamic` instead of `object`? – Yuval Itzchakov Sep 29 '15 at 13:05
  • @Yuval: The plan is to take different key,values and values could be any data type. However just realized Specflow passes in all table values as Strings so this solution wont work. e.g. Given I have following values |FieldKey|FieldValue| |InitialAmount|1000.00| |Currency|"GBP"| – DesertRose Sep 29 '15 at 13:11
  • You'll need to parse out the values beforehand than. – Yuval Itzchakov Sep 29 '15 at 13:14

1 Answers1

0

Use request.RequestParameters

var result = client.PostAsJsonAsync<Dictionary<string, dynamic>>(_address.ToString(), request.RequestParameters).Result;
ram hemasri
  • 1,624
  • 11
  • 14
  • Now I am getting this error Error 4 'System.Net.Http.HttpClient' does not contain a definition for 'PostAsJsonAsync' and the best extension method overload 'System.Net.Http.HttpClientExtensions.PostAsJsonAsync(System.Net.Http.HttpClient, string, T)' has some invalid arguments Error 5 Argument 3: cannot convert from 'System.Collections.Generic.Dictionary' to 'Go.RequestBuilders.Request' C:\AcceptanceTestSteps\ServiceCallSteps.cs – DesertRose Sep 29 '15 at 13:01
  • @DesertRose You need : PostAsJsonAsync>(...) instead? – Siva Gopal Sep 29 '15 at 13:02
  • @Ram: Thanks for your soln. – DesertRose Sep 29 '15 at 13:05