0

I am trying to send the HTTP post request using HTTP client.while setting my Content-Type header with StringContent method, I am getting the error:

the format "application/json;ty=2 "is incorrect

My code is as follows:

string result2 = JsonConvert.SerializeObject(values);
 var content = new StringContent(result2, Encoding.UTF8, "application/json; ty=2");
 var response = await client.PostAsync("http://127.0.0.1:8080/~/in-cse/in-name", content);
                    var response_string = await response.Content.ReadAsStringAsync();

But in my HTTP Post format I have to include ty=2 parameter along with application/json. I have tested this with Postmanand it worked perfectly. How can I achieve the same thing here. I also even found you can add parameter to content type in the following format: content := "Content-Type" ":" type "/" subtype *(";" parameter) then why is it not working for me. **EDIT:**even tried this:

 HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "http://127.0.0.1:8080/~/in-cse/in-name");
                    request.Content = new StringContent(result2, Encoding.UTF8, "application/json;ty=2");

                    client.SendAsync(request).ContinueWith(resposetask => { Console.WriteLine("response:{0}", resposetask.Result); });

same error i am getting

pranjal khanduri
  • 351
  • 1
  • 3
  • 16

1 Answers1

0

You will have to use TryAddWithoutValidation in DefaultRequestHeaders in HttpClient like following:

client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json;ty=2");
FaizanHussainRabbani
  • 3,256
  • 3
  • 26
  • 46