I'm trying to POST a JsonObject using HttpClient from Web API. I'm not quite sure how to go about this and can't find much in the way of sample code.
Here's what I have so far:
var myObject = (dynamic)new JsonObject();
myObject.Data = "some data";
myObject.Data2 = "some more data";
HttpClient httpClient = new HttpClient("myurl");
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = httpClient.Post("", ???);
I think I need to cast my JsonObject as a StreamContent but I'm getting hung up on that step.
With the new version of HttpClient and without the WebApi package it would be:
var content = new StringContent(jsonObject.ToString(), Encoding.UTF8, "application/json");
var result = client.PostAsync(url, content).Result;
Or if you want it async:
var result = await client.PostAsync(url, content);
The easiest way is to use a StringContent, with the JSON representation of your JSON object.
httpClient.Post(
"",
new StringContent(
myObject.ToString(),
Encoding.UTF8,
"application/json"));
Depending on your .NET version you could also use HttpClientExtensions.PostAsJsonAsync method.
https://msdn.microsoft.com/en-us/library/system.net.http.httpclientextensions.postasjsonasync.aspx
If using Newtonsoft.Json:
using Newtonsoft.Json;
using System.Net.Http;
using System.Text;
public static class Extensions
{
public static StringContent AsJson(this object o)
=> new StringContent(JsonConvert.SerializeObject(o), Encoding.UTF8, "application/json");
}
Example:
var httpClient = new HttpClient();
var url = "https://www.duolingo.com/2016-04-13/login?fields=";
var data = new { identifier = "username", password = "password" };
var result = await httpClient.PostAsync(url, data.AsJson())
I don't have enough reputation to add a comment on the answer from pomber so I'm posting another answer. Using pomber's approach I kept receiving a "400 Bad Request" response from an API I was POSTing my JSON request to (Visual Studio 2017, .NET 4.6.2). Eventually the problem was traced to the "Content-Type" header produced by StringContent() being incorrect (see https://github.com/dotnet/corefx/issues/7864).
tl;dr
Use pomber's answer with an extra line to correctly set the header on the request:
var content = new StringContent(jsonObject.ToString(), Encoding.UTF8, "application/json");
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var result = client.PostAsync(url, content).Result;
I spent hours trying to solve this.
But #anthls anwser saved my skin.
var data = new StringContent(JsonConvert.SerializeObject(new
{
abc = "jsjs",
xyz = "hhhh"
}));
data.Headers.ContentType = new MediaTypeHeaderValue("application/json"); // <--
var response = await client.PostAsync(url, data);
the code over it in vbnet:
dim FeToSend as new (object--> define class)
Dim client As New HttpClient
Dim content = New StringContent(FeToSend.ToString(), Encoding.UTF8,"application/json")
content.Headers.ContentType = New MediaTypeHeaderValue( "application/json" )
Dim risp = client.PostAsync(Chiamata, content).Result
msgbox(risp.tostring)
Hope this help
Thank you pomber but for
var result = client.PostAsync(url, content).Result;
I used
var result = await client.PostAsync(url, content);
because Result makes app lock for high request
I Faced same issue i.e
var content = new StringContent(jsonObject.ToString(), Encoding.UTF8, "application/json");
gave
"400 Bad Request"
Serializing JsonObject separately and passing the string in StringContent() solved issue for me, no need to set Encoding.UTF8 separately.
I want to answer all in one response when doing this job as a note for all and myself:
According to Serez's answer HttpContent derived classes list as below
https://stackoverflow.com/a/42380027/914284
HttpClient PostAsync has some background depending on the context you working on!
You can post data by the type that you want to send to server in cases
Server context waits it as bellow
[HttpPost]
public async Task<IActionResult> Submit(MyModel model)
[HttpPost]
public async Task<IActionResult> Submit([FromForm] MyModel model)
[HttpPost]
public async Task<IActionResult> Submit([FromBody] MyModel model)
When writing FromForm or Body it has working as FromForm.
FromBody needs json content otherwise it requires KeyValuePairs as rows. There is some implementations for both of them such as below:
For FromForm: I have used an extension
public static class HelperExtensions
{
public static FormUrlEncodedContent ToFormData(this object obj)
{
var formData = obj.ToKeyValue();
return new FormUrlEncodedContent(formData);
}
public static IDictionary<string, string> ToKeyValue(this object metaToken)
{
if (metaToken == null)
{
return null;
}
// Added by me: avoid cyclic references
var serializer = new JsonSerializer { ReferenceLoopHandling = ReferenceLoopHandling.Ignore };
if (metaToken is not JToken token)
{
// Modified by me: use serializer defined above
return ToKeyValue(JObject.FromObject(metaToken, serializer));
}
if (token.HasValues)
{
var contentData = new Dictionary<string, string>();
foreach (var child in token.Children().ToList())
{
var childContent = child.ToKeyValue();
if (childContent != null)
{
contentData = contentData.Concat(childContent)
.ToDictionary(k => k.Key, v => v.Value);
}
}
return contentData;
}
var jValue = token as JValue;
if (jValue?.Value == null)
{
return null;
}
var value = jValue?.Type == JTokenType.Date ?
jValue?.ToString("o", CultureInfo.InvariantCulture) :
jValue?.ToString(CultureInfo.InvariantCulture);
return new Dictionary<string, string> { { token.Path, value } };
}
}
For FromBody: Use any json converter library Newtonsoft or microsoft
using Newtonsoft.Json;
var jsonString = JsonConvert.SerializeObject(obj);
In both of them, content type should be defined according the requirement, for example for json (Write to header)
request.Headers.Accept.Clear();
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
or another usage
using (var content = new StringContent(JsonConvert.SerializeObject(answer), System.Text.Encoding.UTF8, "application/json"))
{
var answerResponse = await client.PostAsync(url, content);
//use await it has moved in some context on .core 6.0
}
If you should use authorization on the context also you can provide authorization as below:
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "Your Oauth token");
I'm working on an application to get some API experience with C#. I'm pulling data from a site involving jokes, but no matter what I try I can't seem to get the actual response.
I've gone over several different methods, this is the farthest I've gotten. I'm using the RestSharp library, and it's returning RestSharp.RestResponse. I've tried several different methods of deserializing this as that's what I believe is needed. From the API I'm using, the default response format is text/html. Any tips on extracting this to a string containing the joke itself would be most appreciated.
public void CreateJoke()
{
var client = new RestClient("https://icanhazdadjoke.com/");
client.AddDefaultHeader("user-agent", "Dadbot");
var request = new RestRequest("https://icanhazdadjoke.com/");
var response = client.Get(request);
lblJoke.Text = response.ToString();
}
Expected result: getting a string I can put into a label. Actual result: RestSharp.RestResponse.
Try instead accessing the Content property of response:
public void CreateJoke()
{
var client = new RestClient("https://icanhazdadjoke.com/");
client.AddDefaultHeader("user-agent", "Dadbot");
var request = new RestRequest("https://icanhazdadjoke.com/");
var response = client.Get(request);
lblJoke.Text = response.Content;
}
Hopefully that helps!
I guess this is what you want.
public string CreateJoke()
{
var request = (HttpWebRequest)WebRequest.Create("https://icanhazdadjoke.com/");
request.Method = "GET";
request.Accept = "text/plain";
var jokeResponse = request.GetResponse();
var joke = string.Empty;
using (var sr = new StreamReader(jokeResponse.GetResponseStream()))
{
joke = sr.ReadToEnd();
}
//Console.WriteLine(joke);
return joke;
}
recently i discovered this amazing cms called Directus, where you can manage your database and Tables with web request and Json.
Everything worked fine creating,updating,reading...till i came to the point where i want to Create (Upload) a Image using WebRequest.
Im basicly reading a image as Base64 and writing the data along with the parameters in the Uri using a simple GET request exactly like described in API.
Regardless what i try and use the Images Never show up in my Files.
Am i doing something wrong or forgetting something?
Or does directus want something else from me?
My first try:
public static async void UploadUserImage() {
var uri = "http://IP/Directus/api/1/files?access_token=SecretApiKey";
var data = GetImageData();
var finalUri = $"{uri}&data={data}";
using (var client = new HttpClient()) {
var responseString = await client.GetStringAsync(finalUri);
Console.Write(responseString);
}
}
My Second try with Json:
public static async void UploadUserImage() {
var uri = "http://IP/Directus/api/1/files?access_token=SecretApiKey";
var data = GetImageData();
var finalUri = $"{uri}&data={data}";
var postModel = new PictureModel {
data = data,
title = "Test",
name = "test"
};
using (var client = new HttpClient())
{
// Serialize our concrete class into a JSON String
var content = JsonConvert.SerializeObject(postModel);
var contenta = new StringContent(content, Encoding.UTF8, "application/json");
var response = await client.PostAsync(finalUri, contenta);
var result = await response.Content.ReadAsByteArrayAsync();
Console.Write(System.Text.Encoding.UTF8.GetString(result));
}
}
The docs is incorrect it's actually a POST request. Thanks for pointing that out.
To upload a new file you need three provide three values:
{
"name": "image.png",
"type": "image/png",
"data": "base64content"
}
The data content has to be in this format data:<mime-type>;base64,<data-content> so it will look something like this: data:image/png;base64,ThisIsABase64Content
We are updating the docs and removing the data:image/png which is unnecessary.
I am developing an website for my company and in there I want to tag skills to specific people. So the programming tags that shows in stackoverflow is a valuable source. SO I want to get the tag db of the stackoverflow.
I found an API for that.
API for the TAGS
So what I am trying to do is read this json string and forloop through the pages to get the tags and save them in a DB.
private static void ReadJson()
{
HttpClient client = new HttpClient();
//DefaultRequestHeader to Json
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//Create an instance of HttpResponse & invoke the service asynchronously
HttpResponseMessage response = client.GetAsync("https://api.stackexchange.com/2.2/tags?page=400&pagesize=100&order=desc&sort=popular&site=stackoverflow").Result;
//Http Status code 200
if (response.IsSuccessStatusCode)
{
//Read response content result into string variable
string JSON = response.Content.ReadAsStringAsync().Result;
//Deserialize the string(JSON) object
var jObj = (JObject)JsonConvert.DeserializeObject(JSON);
//access items from anonymous (Json object) type and add to the list
var result = jObj["items"].Select(item => new
{
name = item["name"]
}).ToList();
//output the data || NOTE: **NSERT into database table**
foreach (var item in result)
{
Console.WriteLine(item.name);
}
}
}
So at string JSON = response.Content.ReadAsStringAsync().Result;
method it shows some weired charachters ( triangels and shapes ) because of that process is stopping there.
0\0\0�Z�n�8\f��<�E/S��,-�cYtuI�\f�ߗf\a�g�
What I am doing wrong here?
If there any way to make this happen please contribute your answer.
Thanks.
What you're getting is a compressed response. So instead of reading it as a string, read it as a byte[], decompress it and you'll find your JSON string.
static async void DoStuff()
{
HttpClient client = new HttpClient();
var bytes = await client.GetByteArrayAsync("https://api.stackexchange.com/2.2/tags?page=400&pagesize=100&order=desc&sort=popular&site=stackoverflow");
var decompressedJson = new StreamReader(new GZipStream(new MemoryStream(bytes), CompressionMode.Decompress)).ReadToEnd();
// decompressedJson will now contain '{"items":[{"has_synonyms":false, .....'
// Continue with deserialization of JSON
}
static void Main(string[] args)
{
Task t = new Task(DoStuff);
t.Start();
Console.WriteLine("Doing stuff");
Console.ReadLine();
}
}
You can continue with the deserialization from there. Keep in mind that the API will throw an error when you're sending too many requests.
I am trying to send http request of json data to a web service. It successfully get directed to the web service but the data is always null...
Here is my web service:
public bool CheckUserExist ([FromBody] string Email)
{
List<User> all_users = repo.getUsers();
var match = all_users.Find(i => i.Email == Email);
if (match == null)
{
return false;
}
else
{
return true;
}
}
and here is my Http Request:
var webAddr = "http://localhost:59305/api/User/CheckUserExist";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
httpWebRequest.ContentType = "application/json; charset=utf-8";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{\"Email\":\"Email\"}";
streamWriter.Write(json);
streamWriter.Flush();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
return RedirectToAction("Index");
}
as i mentioned...Iam using a debugger at the web service function...the request get directed to the service but variable "Email" is always null
The quick fix is to change what you are posting. If you want your API endpoint to work with [FromBody] string Email, then you should change your "json":
string json = "\"a#b.c\"";
streamWriter.Write(json);
streamWriter.Flush();
However, you may want to consider a few other changes for the long term on your approach:
Let your repo return IQueryable instead of List or some other IEnumerable. As written, you will always return every user from the database. With Linq, you can leverage the lazy initialization and let your query only get the matching user instead of get EVERY user and then find the match. This will not scale well.
Use new HttpClient and async functionality in your action instead of HttpWebRequest
Instead of manually building your JSON, let .NET do the work and create a class that gets serialized
With the above solutions in place (except for first since you didn't post your data context, didn't want to make too many assumptions), here are some examples to get you started:
Define a shared user search class (shared either in same project or shared DLL)
public class UserSearch
{
public string Email { get; set; }
}
Let Web API map post against the search class
public bool CheckUserExist([FromBody] UserSearch userSearch)
{
IQueryable<User> all_users = repo.getUsers();
var isMatch = all_users.Any(i => i.Email == userSearch.Email);
return isMatch;
}
Change to HttpClient and use async to send API request with new search class
public async Task<ActionResult> Check()
{
using (var client = new HttpClient())
{
var search = new UserSearch() { Email = "a#b.c" };
var response = await client.PostAsJsonAsync("http://localhost:59305/api/User/CheckUserExist", search);
if (response.IsSuccessStatusCode)
{
bool exists = await response.Content.ReadAsAsync<bool>();
// Handle what to do with exists
return RedirectToAction("Index");
}
else
{
// Handle unsuccessful call
throw new Exception("Application error");
}
}
}
You can't use string to accept the post data, please define a struct class or use dynamic to receive the Json string.