-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

15
  • 2
    Possible duplicate of How do I pass an object to HttpClient.PostAsync and serialize as a JSON body? Commented Sep 28, 2018 at 1: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. Commented Sep 28, 2018 at 1:08
  • 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. Commented Sep 28, 2018 at 1:15
  • 1
    Why am I saying you're not posting JSON? Because I've read your code. Commented Sep 28, 2018 at 1:16
  • 1
    Then why do you say you're posting JSON? Commented Sep 28, 2018 at 1:20

1 Answer 1

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;
                    }
Sign up to request clarification or add additional context in comments.

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.