0

I have a simple stored procedure which get user name and password and SELECT all info and return it.

I have this request code here:

[AcceptVerbs("GET", "POST")]
    public Student GetInfoByLogin(Student studentm)
    {
        //Response response = new Response();
        Student student = new Student();
        try
        {
            Connection();
            SqlCommand cmd = new SqlCommand("GetInfoByUserNameAndPassword", conn);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@UserName", studentm.UserName);
            cmd.Parameters.AddWithValue("@Password", studentm.Password);

            conn.Open();
            SqlDataReader reader =  cmd.ExecuteReader();

            while (reader.Read())
            {

                student.StundentID = Convert.ToInt32(reader["StudentID"]);
                student.UserName = reader["UserName"].ToString();
                student.Password = reader["Password"].ToString();
                student.PhoneNumber = reader["PhoneNumber"].ToString();
                student.Name = reader["Name"].ToString();
                student.Age = Convert.ToInt32(reader["Age"]);
                student.InstitutionName = reader["InstitutionName"].ToString();
                student.RechargedAmount = Convert.ToInt32(reader["RechargedAmount"]);
                student.IsPending = Convert.ToInt32(reader["IsPending"]);

            }
            conn.Close();
        }
        catch (Exception ex)
        {
            //
        }            
        return student;
    }

I tested this request it works fine in Postman. But when I hit the url there are no info. I am thinking that this is ExecuteReader() operation. It should show show something!!! There are an error log like this:

<Message>The request contains an entity body but no Content-Type header. The inferred media type 'application/octet-stream' is not supported for this resource.</Message>

No MediaTypeFormatter is available to read an object of type 'Student' from content with media type 'application/octet-stream'.

But in Postman it works fine. Can you tell me how to sent POST and use GET to get the returned value in single url call.

Postman screenshot:

  • Usually GET takes parameters from the query string, POST from the request body. The student parameter is an object, and can't be read from the query string. Do you have an example of how you call the method ? The error message seems explicit, you should provide a Content-type header application/xml or application/json depending on how you srialize your parameter – B. Lec Nov 26 '19 at 14:36

1 Answers1

0

It sounds like you're wanting to do a one step process with two steps.

You do not POST then GET with an HTTP Request, you POST then get a response. This response might be information to pass along to another endpoint, but all HTTP Requests get a Response of some form, which may contain data.

[AcceptVerbs("POST")]
public Student GetInfoByLogin(Student studentm)
{
   return GetStudentData();
}

The error itself is indicating you're calling your endpoint incorrectly <Message>The request contains an entity body but no Content-Type header. The inferred media type 'application/octet-stream' is not supported for this resource.</Message>

This means that there's nothing wrong with your endpoint itself, it's the caller that is not handling it.

Consider this SO link How do you set the Content-Type header for an HttpClient request?

It's possible that you're doing this wrong on either the read or write side, as you haven't indicated where the error is coming from (caller or handler)

Captain Prinny
  • 459
  • 8
  • 23
  • That means I don't have any problem in endpoint. I have to handle it through caller? Can I call this url but GetAsync()? or I have to call it with PostAsync(); I don't know! I am trying to get some info but sending some parameter. Is there any way to do that? GET only get thing POST only send parameter. I can't make thousand url or database table to get result. – Mahir Muzahid Nov 26 '19 at 16:26
  • @MahirMuzahid I think you should read more about the http protocol [wiki](https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol) you don't seem to understand what requests / responses are – B. Lec Nov 27 '19 at 08:53