1

Currently I am working with HttpClient, but I am unable to understand which parameter I have to pass, i.e. string content or bytes content.

Code 1:

ModelAttribute modelAttribute = new ModelAttribute {Id=modelId, Type="new", MakeId = makeId};
RefreshWrapper refreshWrapper = new RefreshWrapper(){ ModelAttribute = new List<ModelAttribute>{modelAttribute}};
var jsonInString = JsonConvert.SerializeObject(refreshWrapper);
string baseUrl = string.Format("http://localhost:8090/api/abc");
var buffer = System.Text.Encoding.UTF8.GetBytes(jsonInString);
var byteContent = new ByteArrayContent(buffer);
byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpClient client = new HttpClient();
client.BaseAddress = new System.Uri(baseUrl);
var result=client.PostAsync("", byteContent).Result;

Code 2:

ModelAttribute modelAttribute = new ModelAttribute {Id=modelId, Type="new", MakeId = makeId};
RefreshWrapper refreshWrapper = new RefreshWrapper(){ ModelAttribute = new List<ModelAttribute>{modelAttribute}};
var jsonInString = JsonConvert.SerializeObject(refreshWrapper);
string baseUrl = string.Format("http://localhost:8090/apabci/");
HttpClient client = new HttpClient();
client.PostAsync(baseUrl, new StringContent(jsonInString, Encoding.UTF8, "application/json"));

Which one is better?

1
  • 1
    Welcome to Stack Overflow. As there is no appreciable difference between the two methods (internally, the StringContent will be converted to an array of bytes anyway), answers to this question are likely to be opinions, rather than objective facts. Commented Aug 23, 2018 at 20:09

1 Answer 1

1

I always use methods that look like this:

    using System.Net.Http;
    HttpClient client = new HttpClient();
    public async Task<List<Object>> GetObjectAsync()
    {
        try
        {
            string url = "http://yourapiurl.com/";
            var response = await client.GetStringAsync(url);
            var objectsReturned = JsonConvert.DeserializeObject<List<Object>>(response);
            return objectsReturned;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
     public async Task AddObjectAsync(Object object)
    {
        try
        {
            string url = "http://yourapiurl.com/{0}";
            var uri = new Uri(string.Format(url, object.Id));
            var data = JsonConvert.SerializeObject(object);
            var content = new StringContent(data, Encoding.UTF8, "application/json");
            HttpResponseMessage response = null;
            response = await client.PostAsync(uri, content);

            if (!response.IsSuccessStatusCode)
            {
                throw new Exception("Error");
            }
        }
        catch(Exception ex)
        {
            throw ex;
        }
    }

In the PostAsync Method, I send an Serialized Object to the API, {0} is the parameter that will be exchanged for the serialized object.

Sign up to request clarification or add additional context in comments.

4 Comments

An explanation would be in order. Especially when this answer does not appear to directly answer the question.
We like answers on Stack Overflow. We do. But we usually like answers that pertain specifically to the question at hand. In this case, the OP (original post) discusses whether to use a byte array or a string to do an HTTP POST to a server. This answer shows doing an HTTP GET.
Sorry about that, I'm still a New Contributor, I made some edits to the post, does them solve all the issues?
I still don't see how it addresses the question. The OP writes "... string content or bytes content ... Which one is better?", so I would expect an answer to contain at least one of words "string" and "bytes", along with a reason. There is also Heretic Monkey's comments about HTTP GET vs. POST. And why is there deserialisation in your code? - there could be a good reason, but it should be explained.

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.