sioesi
sioesi

Reputation: 517

Http post Angular2

I am migrating an Ionic v1 application to Ionic v2. In one of my methods I make a call to a WCF service. In AngularJS I do it like this:

$http({
 method: 'POST',
 url: url,
 data: JSON.stringify({ dato: 11 }),
 dataType: "json",
 contentType: "application/x-www-form-urlencoded"
})

And in Angular2 like this:

let data = JSON.stringify({ dato: 11 });
let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded'});
let options = new RequestOptions({ headers: headers });
this.http.post(url, data, options)
.subscribe(data => {
  console.log(data);
}, error => {
  console.log("Error");
});

However, I receive from my C# service an error and says that the format I send to my functions is 'RAW' while waiting for it to send JSON with Angular 2

This error occurred to me in AngularJS but it was solved by adding the code I previously left in this post.

EDIT

My WCF service :

[OperationContract]
[WebInvoke(UriTemplate = "/GetCaptaciones", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat=WebMessageFormat.Json , BodyStyle = WebMessageBodyStyle.Wrapped)]
List<Captacion> GetCaptaciones(int id_captador);

AND

let data = JSON.stringify({ id_captador: 11 });
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
this.http.post(url + "/GetCaptaciones", data, options).subscribe(data => {
  console.log(data);
});

Not working,

But if I test from POSTMAN this works

Upvotes: 0

Views: 401

Answers (2)

Navoneel Talukdar
Navoneel Talukdar

Reputation: 4598

In addition to what Darin has said I would say you are not using BodyStyle = WebMessageBodyStyle.Wrapped correctly.

See your wcf method expects a single parameter so you could go ahead without wrappedrequest property.

[OperationContract]
[WebInvoke(UriTemplate = "/GetCaptaciones", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat=WebMessageFormat.Json)]
List<Captacion> GetCaptaciones(int id_captador);

Wrapped request means there should be some wrapper object on top of the input properties which WCF method takes as argument.

So something like this

 var input =
        {
            "CaptacionesInput": 
            {
                "id_captador": 11
            }
        };

Now wcf method becomes like

List<Captacion> GetCaptaciones(CaptacionesInputType CaptacionesInput);

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038820

This:

let data = JSON.stringify({ dato: 11 });

and this:

let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded'});

are terribly contradictory and inconsistent.

Please try to be consistent when you make an HTTP request against a web server about how you are going to encode the request. So if intend to send a JSON body make sure that you specify the proper Content-Type request header, so that the server would know how to process this payload:

let headers = new Headers({ 'Content-Type': 'application/json'});

Upvotes: 1

Related Questions