Postasync Json C#

Asked

Viewed 12,121 times

3

I have the Following class set:

public class Ticket
{
    public string name
    public string content
    public int itilcategories_id
}

And the following code sample:

static HttpClient client = new HttpClient();

client.BaseAddress = new Uri("https://stgsd.primaverabss.com/");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));

 ticket.name = "TESTE";
 ticket.content = "teste";
 ticket.itilcategories_id = 1

 HttpResponseMessage response = await client.PostAsync("apirest.php/Ticket/", XXXXXXXXX );

My goal is to replace the XXXXXXX that are in Postasync so that in its place you pass the information of the Ticket and when sending to the respective link the content is as follows:

{"input":[{"name": "TESTE", "content": "teste", "itilcategories_id":"1"}]}

Any ideas? Any questions, let us know!!

1 answer

14


To reproduce the presented model, you need to create an object that has an attribute called input and that receives an array or list of Tickets. Then you need to serialize this object in a json and pass it as a StringContent() (adding the necessary headers) to your method PostAsync().

Follow an example:

HttpClient client = new HttpClient();

client.BaseAddress = new Uri("https://stgsd.primaverabss.com/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
    new MediaTypeWithQualityHeaderValue("application/json"));

var ticket = new Ticket();
ticket.name = "TESTE";
ticket.content = "teste";
ticket.itilcategories_id = 1;

List<Ticket> tickets = new List<Ticket>();
tickets.Add(ticket);

var parametro = new
{
    input = tickets.ToArray()
};            

var jsonContent = JsonConvert.SerializeObject(parametro); 
var contentString = new StringContent(jsonContent, Encoding.UTF8, "application/json");
contentString.Headers.ContentType = new 
MediaTypeHeaderValue("application/json"); 
contentString.Headers.Add("Session-Token", session_token); 


HttpResponseMessage response = await Client.PostAsync("apirest.php/Ticket/", contentString);
  • include your client’s statement

  • @idkWhy, Adjusted.

  • @Idkwhy, you solved?

  • Gives error 400: Bad Request

  • Are you sure that that is the expected model? via Postman, you can perform the operation?

  • I use Fiddler and yes I performed the operation

  • I just made one and it came out right

  • Pass "application/json" in the Stringcontent constructor and then contentString.Headers.Contenttype = new Mediatypeheadervalue("application/json"); and redundancy, not?

  • @Aitiow yes and is purposeful, already happened in some applications, of the variable not being added when presented in the constructor of StringContent(). So by habit I always reinforce the Mediatype too

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.