-1

I'm trying to POST a data to web using HttpClient but i can't succeed.

Here is my JSON web api

{
    "Categories":[
        {
            "CategoryID":1,
            "Category":"Category 1"
        },
        {
            "CategoryID":2,
            "Category":"Category 2"
        }
    ]
}

i'am sending categories data to web my web developer send me above json to send a data from winform to web

Here is my code

IEnumerable<KeyValuePair<string, string>> paramt = new List<KeyValuePair<string, string>>()
                {
                    new KeyValuePair<string,string>("CategoryID","1"),
                    new KeyValuePair<string,string>("Category","Pizza")
                };
                HttpContent q = new FormUrlEncodedContent(paramt);
                using (HttpClient client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.BaseAddress = new Uri(url);
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", api);
                    HttpResponseMessage response = client.PostAsync("api/categories", q).Result;
 }

sorry for my english moderator please update my question

Naeem Shah
  • 115
  • 1
  • 1
  • 12
  • What is bytecontent? Also, why are you not using async await etc and... What is the problem? Please ask a proper question.. – X39 Sep 28 '18 at 00:42
  • please see again and i want to send to data to web – Naeem Shah Sep 28 '18 at 00:46
  • You haven't described the problem. Also, I don't see any evidence of you converting your data to JSON. You're sending it as `FormUrlEncodedContent` which should set the content-type to `x-www-form-urlencoded` and create a body like `CategoryID=1&Category=Pizza` based on your code. – ProgrammingLlama Sep 28 '18 at 01:00
  • 2
    Possible duplicate of [How do I pass an object to HttpClient.PostAsync and serialize as a JSON body?](https://stackoverflow.com/questions/36625881/how-do-i-pass-an-object-to-httpclient-postasync-and-serialize-as-a-json-body) – ProgrammingLlama Sep 28 '18 at 01:05
  • 1
    Two things: 1. Only people whose names end with ♦ and whose profile says `(moderator)` are moderators. The rest of us are just users like yourself. 2. You haven't described the problem and why your code isn't working. You have only described what you're trying to do, not why you've posted a question. Despite this, I think I understand the issue, as I've described above. The linked duplicate shows you how to post JSON (as opposed to a form post) as you're currently doing. – ProgrammingLlama Sep 28 '18 at 01:08
  • {"CategoryID":1,"Category":"Category 1"} my code works perfectly with this json only not working with nested json object like which i shown above – Naeem Shah Sep 28 '18 at 01:14
  • 1
    You are not posting JSON, you are posting a form body. You are not posting JSON, you are posting a form body. You are not posting JSON, you are posting a form body. You are not posting JSON, you are posting a form body. See the duplicate I linked, which shows how to post **JSON**. – ProgrammingLlama Sep 28 '18 at 01:15
  • 1
    Why am I saying you're not posting JSON? Because I've read your code. – ProgrammingLlama Sep 28 '18 at 01:16
  • StatusCode: 500, ReasonPhrase: 'Internal Server Error', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Transfer-Encoding: chunked Connection: keep-alive Keep-Alive: timeout=15 X-RateLimit-Limit: 60 X-RateLimit-Remaining: 58 – Naeem Shah Sep 28 '18 at 01:18
  • yes m posting a form body – Naeem Shah Sep 28 '18 at 01:18
  • 1
    Then why do you say you're posting JSON? – ProgrammingLlama Sep 28 '18 at 01:20
  • because m working with web api first time – Naeem Shah Sep 28 '18 at 01:22
  • OK Well the duplicate will show you how to send JSON. Just follow that. Also note that if you serialize `paramt` to JSON at the moment, you'll get `[{"Key":"CategoryID","Value":"1"},{"Key":"Category","Value":"Pizza"}]`. An example of how to get the JSON you require can be seen [here](http://rextester.com/MEMN11145). – ProgrammingLlama Sep 28 '18 at 01:25
  • john thannnnnnnnnnnnnnnnnnnnnkkkkkkkkkkkkk youuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu this is working – Naeem Shah Sep 28 '18 at 01:36
  • post as a Answer i will mark for others – Naeem Shah Sep 28 '18 at 01:36

1 Answers1

1

Thanks @John with the help of yours i did this

public class CategoryItem
        {
            public int CategoryID { get; set; }
            public string Category { get; set; }
        }

        public class CategoriesRoot
        {
            public IList<CategoryItem> Categories { get; set; }
        }

         var tmp = new CategoriesRoot
                {
                    Categories = new List<CategoryItem> {
                    new CategoryItem { CategoryID = 1, Category = "Pizza" }
                }
                };

                using (HttpClient client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.BaseAddress = new Uri(url);
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", api);
                    HttpResponseMessage response = client.PostAsJsonAsync("api/categories", tmp).Result;
                    }
Naeem Shah
  • 115
  • 1
  • 1
  • 12