Search code examples
asp.netangularweb-servicesasmx

Send a request to Asp.net WebService using Angular


I executed a WebService using Postman and it's executed correctly. And now i want to send a request to the WebService using Angular

Here is the Asp.net Web Service

This is the code for my WebService

public class Temperature : System.Web.Services.WebService
    {
        [WebMethod]
        public double Farenheit(double celsius)
        {
            return  (celsius * 9) / 5 + 32;
        }
        [WebMethod]
        public double Celsius(double fahrenheit)
        {
            return (fahrenheit - 32) * 5 / 9;
        }
    }

And this is the screenshot on how i send a request using PostMan and it's working as expected

Screenshot for Calling the WebService using Postman

Here is the code for the Angular

const httpOptions = {
      headers: new HttpHeaders({
        'Content-Type':  'text/xml',

      })
    };


this.http.post('https://localhost:44389/Temperature.asmx/Celsius', '50',  httpOptions)
    .subscribe(res => {
      console.log('Data: ' + res);
    })

And this is the error I'm receiving

Error

"System.InvalidOperationException: Request format is invalid: text/xml.
   at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
   at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
"

Message

Http failure response for https://localhost:44389/Temperature.asmx/Celsius: 500 OK

Name

"HttpErrorResponse"

Solution

  • Change your 'Content-Type': 'text/xml' to 'Content-Type': 'application/json'