Home > Mobile >  one api returns: Method not allowed when sending webservice request
one api returns: Method not allowed when sending webservice request

Time:12-28

I have a working webservice running where I have just one api call, it works perfect.
So now I want to add another api but I cannot get this one to work, it keeps returning this error when I try in in a browser

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

If I try it in my testclient c# project it returns Method not allowed

I am doing exact the same as in the already working api, so what can be the reason for this nasty error ?

code from the working api

public class SCSController : ApiController
{
    // GET: SCS
    public HttpResponseMessage AddDriverPayments(DriverPayment driverPayments)
        {
            HttpResponseMessage result = null;

            try
            {
                DriverPaymentResponse response = new DriverPaymentResponse() { PaymentID = driverPayments.newID, SCS_ID = driverPayments.sCS_ID };
                result = Request.CreateResponse(response);
            }
            catch (Exception ex)
            {
                var resp = new HttpResponseMessage(HttpStatusCode.InternalServerError)
                {
                    Content = new StringContent("error"),
                    ReasonPhrase = ex.Message
                };
                throw new HttpResponseException(resp);
            }

            return result;
        }

code from the not working api

    public class VLAController : ApiController
    {

        // GET: VLA
        public HttpResponseMessage GetAppLogin(AppLoginRequest appLoginRequest)
        {
            HttpResponseMessage result = null;

            try
            {
                //VLADataBase db = new VLADataBase();
                //AppLoginResponse response = db.GetAppLogin(appLoginRequest);
                AppLoginResponse response = new AppLoginResponse() { Authorized = true, TruckID = 123 };
                result = Request.CreateResponse(response);
            }
            catch (Exception ex)
            {
                var resp = new HttpResponseMessage(HttpStatusCode.InternalServerError)
                {
                    Content = new StringContent("error"),
                    ReasonPhrase = "fout: "   ex.Message
                };
                throw new HttpResponseException(resp);
            }

            return result;
        }

how I call it in my testclient

This is how I call the working api

  string JsonData = JsonConvert.SerializeObject(payments);
            System.Net.Http.StringContent restContent = new StringContent(JsonData, Encoding.UTF8, "application/json");
            HttpClient client = new HttpClient();
            try
            {
                var response = await client.PostAsync(comboBoxEditWebService.Text, restContent);

                if (response.IsSuccessStatusCode)
                {
                    var stream = await response.Content.ReadAsStringAsync();
                    DriverPaymentResponse Result = JsonConvert.DeserializeObject<DriverPaymentResponse>(stream);

                    textBoxResult.Text = Result.SCS_ID   " "   Result.PaymentID;
                }
                else
                {
                    textBoxResult.Text = response.StatusCode   " "   response.ReasonPhrase;
                }
            }
            catch (Exception ex)
            {
                textBoxResult.Text = ex.Message;
            }

And this is how I call the not working api

   string JsonData = JsonConvert.SerializeObject(appLoginRequest);
            System.Net.Http.StringContent restContent = new StringContent(JsonData, Encoding.UTF8, "application/json");
            HttpClient client = new HttpClient();
            try
            {
                var response = await client.PostAsync(comboBoxEdit1.Text, restContent);

                if (response.IsSuccessStatusCode)
                {
                    var stream = await response.Content.ReadAsStringAsync();
                    AppLoginResponse Result = JsonConvert.DeserializeObject<AppLoginResponse>(stream);

                    textBox1.Text = Result.Authorized.ToString()   " "   Result.TruckID.ToString(); //Result.SCS_ID   " "   Result.PaymentID;
                }
                else
                {
                    textBox1.Text = response.StatusCode   " "   response.ReasonPhrase;
                }
            }
            catch (Exception ex)
            {
                textBox1.Text = ex.Message;
            }

I must note that both api's are in different units, in different controllers, maybe that can be a problem ?

enter image description here

So how can I get this solved so I can use both api's ?

CodePudding user response:

Decorating the endpoint with [HttpPost] did the trick (see conversation in comments).

  • Related