How to Post Data Using Httpclient

POST data using HttpClient

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;
}

Send form-data in C# HttpClient

You're sending your data in an incorrect way by using FormUrlEncodedContent.

To send your parameters as MultipartFormDataContent string values you need to replace the code below:

var dataContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("key1", "myvalue1"),
new KeyValuePair<string, string>("key2", "myvalue2"),
new KeyValuePair<string, string>("key3", "myvalue3")
});

With this:

content.Add(new StringContent("myvalue1"), "key1");
content.Add(new StringContent("myvalue2"), "key2");
content.Add(new StringContent("myvalue3"), "key3");

How to post data using HttpClient? (an answer than actually works)

Most deadlock scenarios with asynchronous code are due to blocking further up the call stack.

By default await captures a "context" (in this case, a UI context), and resumes executing in that context. So, if you call an async method and the block on the task (e.g., GetAwaiter().GetResult(), Wait(), or Result), then the UI thread is blocked, which prevents the async method from resuming and completing.

.NET HttpClient. How to POST string value?

using System;
using System.Collections.Generic;
using System.Net.Http;

class Program
{
static void Main(string[] args)
{
Task.Run(() => MainAsync());
Console.ReadLine();
}

static async Task MainAsync()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:6740");
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("", "login")
});
var result = await client.PostAsync("/api/Membership/exists", content);
string resultContent = await result.Content.ReadAsStringAsync();
Console.WriteLine(resultContent);
}
}
}

Posting a dictionary as data to HTTPS server in C# using HTTPClient

Try using this approach, it works with the server I am sending data to.

var content = new StringContent("{some:json}");
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

client.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json;"));

HttpResponseMessage response = await client.PostAsync("https://SERVER_ADRESS", content);

I suspect the request header is the reason, and send StringContent instead of FormUrlEncodedContent

How to pass data to api using httpclient post method

try to change your code to this

insert_data(input){
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};

return this.httpClient.post('http://localhost/tasker/api/index.php/insert_users',
{
data:input,
tt:'tt'
}, httpOptions);

}
}

Post method don't need to subscribe. You have to subscribe in the service your call like

    return this.insertDataService.inputData.subscribe();

Please let me know if you still have problem



Related Topics



Leave a reply



Submit