locked
"StatusCode: UnsupportedMediaType, Content-Type: application/json; charset=utf-8, Content-Length: 800)" RRS feed

  • Question

  • User1897897189 posted

    Dear all,

    Getting the following error message when posting data

    "StatusCode: UnsupportedMediaType, Content-Type: application/json; charset=utf-8, Content-Length: 800)"

    var client = new RestClient("http://abc-srvr/PostDetails");
    RestRequest request = new RestRequest(Method.POST);
    request.AddHeader("Content-Type", "application/json");
    request.AddHeader("cache-control", "no-cache");
    request.AddHeader("Accept", "application/json");
    request.AddParameter("undefined", "{ \r\n\"Code\":'" + Session["Code"].ToString() + "',\r\n\"RqID\": '" + Session["RqID"].ToString() + "'\r\n}", ParameterType.RequestBody);
    //request.AddParameter("undefined", "{\n \"Code\": \"123456789\",\n \"RqID\": \"1234\"\n}", ParameterType.RequestBody);
    IRestResponse response = client.Execute(request);

    Thanks

    Tuesday, January 7, 2020 3:27 PM

All replies

  • User475983607 posted

    The POST body is formatted as application/x-www-form-urlencoded not application/json.  The body looks like this...

    undefined={ "Code":'Code',"RqID": 'RqID'}

    You'll need to read the RestClient docs to get the format correct.

    https://github.com/MelbourneDeveloper/RestClient.Net

    Tuesday, January 7, 2020 3:49 PM
  • User1897897189 posted

    taken the below from the postman.

    var client = new RestClient("http://abc-srvr/PostDetails");
    RestRequest request = new RestRequest(Method.POST);
    request.AddHeader("Content-Type", "application/json");
    request.AddHeader("cache-control", "no-cache");
    request.AddHeader("Accept", "application/json");
    request.AddParameter("undefined", "{ \r\n\"Code\":'" + Session["Code"].ToString() + "',\r\n\"RqID\": '" + Session["RqID"].ToString() + "'\r\n}", ParameterType.RequestBody);
    //request.AddParameter("undefined", "{\n \"Code\": \"123456789\",\n \"RqID\": \"1234\"\n}", ParameterType.RequestBody);
    IRestResponse response = client.Execute(request);

    so where should i put your code

    undefined={ "Code":'Code',"RqID": 'RqID'}
    Tuesday, January 7, 2020 3:53 PM
  • User475983607 posted

    taken the below from the postman.

    var client = new RestClient("http://abc-srvr/PostDetails");
    RestRequest request = new RestRequest(Method.POST);
    request.AddHeader("Content-Type", "application/json");
    request.AddHeader("cache-control", "no-cache");
    request.AddHeader("Accept", "application/json");
    request.AddParameter("undefined", "{ \r\n\"Code\":'" + Session["Code"].ToString() + "',\r\n\"RqID\": '" + Session["RqID"].ToString() + "'\r\n}", ParameterType.RequestBody);
    //request.AddParameter("undefined", "{\n \"Code\": \"123456789\",\n \"RqID\": \"1234\"\n}", ParameterType.RequestBody);
    IRestResponse response = client.Execute(request);

    so where should i put your code

    undefined={ "Code":'Code',"RqID": 'RqID'}

    You misunderstand.  your code is generating a content-type application/x-www-form-urlencoded with a content length of 800 which is not correct.  

    Are you using a PostMan plugin to create source code?

    Tuesday, January 7, 2020 4:18 PM
  • User-719153870 posted

    Hi nicklibee,

    taken the below from the postman.

    var client = new RestClient("http://abc-srvr/PostDetails");
    RestRequest request = new RestRequest(Method.POST);
    request.AddHeader("Content-Type", "application/json");
    request.AddHeader("cache-control", "no-cache");
    request.AddHeader("Accept", "application/json");
    request.AddParameter("undefined", "{ \r\n\"Code\":'" + Session["Code"].ToString() + "',\r\n\"RqID\": '" + Session["RqID"].ToString() + "'\r\n}", ParameterType.RequestBody);
    //request.AddParameter("undefined", "{\n \"Code\": \"123456789\",\n \"RqID\": \"1234\"\n}", ParameterType.RequestBody);
    IRestResponse response = client.Execute(request);

    Below is what i got from postman:

    var client = new RestClient("http:          /about.aspx");
    client.Timeout = -1;
    var request = new RestRequest(Method.POST);
    request.AddHeader("Content-Type", "application/json");
    request.AddHeader("Cache-Control", "no-cache");
    request.AddHeader("Accept", "application/json");
    request.AddParameter("application/json", "{\"Code\":\"123456789\",\"RqID\": \"1234\"}", ParameterType.RequestBody);
    IRestResponse response = client.Execute(request);
    Console.WriteLine(response.Content);

    And here's the request header and body:

    Your requestbody might be incorrect, please share if it's possible, it seems not json format.

    Best Regard,

    Yang Shen

    Wednesday, January 8, 2020 5:26 AM