0

I'm having some issues with calling an API. I need to send post data containing 2 things: an ID and an array of int. I have tried a lot of things, all resulting in errors or simply not sending data in the right way. All answers I found, do not handle the fact that I want to send 2 different data types.

On the other side I have:

Call<DefaultResponseList> someMethod(@Field("parm1") String parm1, @Field("parm2[]") ArrayList<Integer> parm2);

I tried:

List<int> ints1 = new List<int>();
ids.Add(1);
ids.Add(2);
ids.Add(3);

var values = new List<KeyValuePair<string, object>>
{
    new KeyValuePair<string, object>("parm1", "lkdjfowejfd123"),
    new KeyValuePair<string, object>("parm2", ints1)

};

var httpClient = new HttpClient(new HttpClientHandler());
HttpResponseMessage response = await httpClient.PostAsync(someUrl, new FormUrlEncodedContent(values));
response.EnsureSuccessStatusCode();

string data = await response.Content.ReadAsStringAsync();            
2
  • 1
    You should know someUrl's environment and then arrange your values according to this information. Ex if target environment is a Apache/PHP server your keys should be like param2[], for classic ASP.NET envrionment values should have same key name with occurence, and if its ASP.NET MVC your key should contains indices like param2[0],param2[1] etc. Commented Sep 17, 2016 at 21:43
  • Target environment is a Apache/PHP server. Commented Sep 18, 2016 at 6:34

2 Answers 2

5

I solved the problem. Thanks to Cihan Uygun for his comments.

List<int> ids = new List<int>();
ids.Add(1);
ids.Add(2);
ids.Add(3);

var values = new List<KeyValuePair<string, string>>
{
    new KeyValuePair<string, string>("parm1", "lkdjfowejfd123")
};

int counter = 0;
foreach (int i in ids)
{
    values.Add(new KeyValuePair<string, string>("parm2[" + counter.ToString() + "]", i.ToString()));
    counter++;
}

var httpClient = new HttpClient(new HttpClientHandler());
HttpResponseMessage response = await httpClient.PostAsync(urlSaveRegions, new FormUrlEncodedContent(values));
response.EnsureSuccessStatusCode();

string data = await response.Content.ReadAsStringAsync();
Sign up to request clarification or add additional context in comments.

Comments

0

Post an integer array using StringContext

var myData = new { myvar = "my string", data = new[] {1,2,3,4,5}};

var httpClient = new HttpClient(new HttpClientHandler());

var jsonObject = JsonConvert.SerializeObject(myData)
var stringContent = new StringContent(jsonObject.ToString(), System.Text.Encoding.UTF8, "application/json");

HttpResponseMessage response = await httpClient.PostAsync(urlSaveRegions, stringContent);
response.EnsureSuccessStatusCode();

string data = await response.Content.ReadAsStringAsync();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.