HOw to POST parameters to Web API using url


#1

Hi,

I need to develop an App which need to pass data to web Api using url

Ex: Url : http://api.app.com/user/register

to above should URL my app should pass user name, password etc as JSON how to do this…???


#2
import { Http, Headers, RequestOptions, Response } from '@angular/http';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/timeout';

...

// Set post
let post = {};

// Set headers
let headers = {};

// Set request options
let RequestOpts = new RequestOptions({ headers: new Headers(headers) });

// Call backend
this.http.post(url, post, RequestOpts)
	.timeout(options.timeout)
	.map((res: Response) => res.json());

#3
import { Http, Headers, URLSearchParams, Request, RequestOptions, RequestMethod } from '@angular/http';
     let headers = new Headers();
        headers.append('HeaderKey', HeaderValue);
        headers.append('HeaderKey',HeaderValue);
        let params: URLSearchParams = new URLSearchParams();
        params.set('paramKey', paramValue);
        params.set('paramKey', paramValue);
        let options = new RequestOptions({
          method: RequestMethod.Post,
          url: this.global.yourAPIurl+ extended URL+"parameters you want to pass",
          search: params,
          body: this.yourBody,
          headers: headers
        });
        this.http.request(new Request(options))
          .map(res => res.json())
          .subscribe(data => {
    }, err => {
            console.log("ERROR!: ", err);
          });

this POST method call would work for you


#4

This is my form data home.html

 <form [formGroup]="form" (ngSubmit)="submit(form.value)">
   <ion-card text-center class="registercard">

     <ion-title class="title">
       Join with KisanRaja
     </ion-title>
     
      <ion-list>

      <ion-item>
      <ion-label floating>First Name</ion-label>   
      <ion-input type="text" [formControl]="form.controls['firstname']"></ion-input>
      </ion-item>  

      <ion-item>
      <ion-label floating>Last Name</ion-label>   
      <ion-input type="text" [formControl]="form.controls['lastname']"></ion-input>
      </ion-item>

      <ion-item>
      <ion-label floating>Email</ion-label>   
      <ion-input type="email" [formControl]="form.controls['email']"></ion-input>
      </ion-item>

      <ion-item>
      <ion-label floating>Mobile</ion-label>   
      <ion-input type="tel" [formControl]="form.controls['phone']"></ion-input>
      </ion-item>

      <ion-item>
      <ion-label floating>Password</ion-label>   
      <ion-input type="password" [formControl]="form.controls['password']"></ion-input>
      </ion-item>

      <ion-item>
      <ion-label>I accept terms and conditions</ion-label>
      <ion-checkbox color="dark" checked="true" [formControl]="form.controls.checked" ></ion-checkbox>
      </ion-item>

      </ion-list>

      </ion-card>


        <!-- Float the icon left -->
        <button ion-button icon-left outline [disabled]="!form.valid" >LOG IN</button>
        </form>

This is my home.ts

  form: FormGroup;
  localUser={firstname:'',lastname:'',email:'',phone:'',password:'',checked:false};
  result:any;
  constructor(public navCtrl: NavController, fbld: FormBuilder,public navParams: NavParams, public alertCtrl:AlertController) {

    this.form = fbld.group({
        'firstname': ['', Validators.compose([Validators.required])],
        'lastname': ['', Validators.compose([Validators.required])],
        'email': ['', Validators.compose([Validators.required, Validators.pattern('^[a-zA-Z0-9-_]+@[a-zA-Z]+\.[a-zA-Z]{2,4}$')])],
        'phone': ['',Validators.compose([Validators.required,Validators.minLength(10)])],
        'password':['',Validators.compose([Validators.required])],
        'checked':[null, Validators.required]
      });
      
  }

this much i did but i am not getting how to move forword


#5

what is this.yourBody in this.