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
How to "Flatten" or "Index" 3D-Array in 1D Array
Most Efficient Way to Insert Rows into MySQL Database
C# Base64 String to Jpeg Image
Understanding .Asenumerable() in Linq to SQL
Why Does (Int)(Object)10M Throw "Specified Cast Is Not Valid" Exception
Does List<T> Guarantee Insertion Order
How to Remove All White Space from the Beginning or End of a String
How to Get the File Size in C#
How Much Memory Does a C#/.Net Object Use
Send HTML Email via C# with Smtpclient
Will the Dynamic Keyword in C#4 Support Extension Methods