Reputation: 37
This is my rest.php
file
<?php
chdir('../../..');
require_once 'SugarWebServiceImplv4_1_custom.php';
$webservice_path = 'service/core/SugarRestService.php';
$webservice_class = 'SugarRestService';
$webservice_impl_class = 'SugarWebServiceImplv4_1_custom';
$registry_path = 'custom/service/v4_1_custom/registry.php';
$registry_class = 'registry_v4_1_custom';
$location = 'custom/service/v4_1_custom/rest.php';
require_once 'service/core/webservice.php';
This is my SugarWebServiceImplv4_1_custom.php file where i have written custom methods
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
if(!defined('sugarEntry')){
define('sugarEntry', true);
}
require_once 'service/v4_1/SugarWebServiceImplv4_1.php';
class SugarWebServiceImplv4_1_custom extends SugarWebServiceImplv4_1
{
public function custom_test($username)
{
$arr = array ('a'=>$username,'b'=>22,'c'=>32,'d'=>44,'e'=>55);
return json_encode($arr);
die;
}
}
This is my registry.php file where i have registered my custom method
<?php
require_once 'service/v4_1/registry.php';
class registry_v4_1_custom extends registry_v4_1
{
protected function registerFunction()
{
parent::registerFunction();
$this->serviceClass->registerFunction('custom_test',
array(
'username'=>'xsd:string),
array(
'return'=>'tns:get_array')
);
}
}
The problem is when i am passing the the data through get method Like this
{"username":"some username"}
i am getting the result but i dont know how to pass it through post method through IOS application. I tried to pass it but I am not getting anything in username. I checked the response through curl as well , it is working using curl, But i have to connect it to IOS. Help will be appreciated
Actually we are building a Hybrid app for IOS using Angular 5 and Ionic 3 Here is the code auth-services.ts
public login(credentials){
if(credentials.username === null || credentials.password === null){
return Observable.throw("Please enter credentials");
} else {
this.username1 = credentials.username;
this.password1 = credentials.password;
return Observable.create(observer =>{
// At this point make a request to your backend to make a real check!
this.method1 = "custom_test";
this.inputType = "JSON";
this.responseType = "JSON";
this.encryptionValue = "PLAIN";
this.bodyData = {}; //get method calling
console.log(JSON.stringify(this.bodyData));
//Sending the Username and Password to the Web Server for authentication. Change the URL Get the response message
this.servicesProvider.restApi("post","http://exmaple.com/custom/service/v4_1_custom/rest.php",this.bodyData).then(
(res) => {
console.log("Response stringify :",JSON.stringify(res));
console.log("Response parse :", res);
console.log("Status :",res.status);
this.response = res.status; //TODO: Replace res.username with res.message as we have to check for user exist or not.
if(this.response == "success out") {
this.success = true;
this.storage.set("status",this.response); //Username value stored in localstorage
this.currentUser = new User('Simon', '[email protected]');
observer.next(this.success);
observer.complete();
} else {
this.success = false;
observer.next(this.success);
observer.complete();
}
}
);
}
Here is the services.ts file. this is a common rest api file for sending rest api requests.
restApi(method,url,data) {
console.log("inside restApi");
switch(method) {
case 'post' : {
console.log("Inside Post Method");
/*
return this.httpClient.post(url,data)
.subscribe(
(res:any) => {
console.log("POST response below");
console.log(res.username);
this.responseData = JSON.stringify(res);
console.log("ResponseData Value");
console.log(this.responseData);
return this.responseData;
}); */
let headers = new Headers({'content-type':'application/json'});
let options = new RequestOptions({ headers:this.headers });
this.responseFromFunction = this.http.post(url,data).toPromise()
.then(this.extractData)
.catch(this.handleError);
break;
}
case 'get' : {
console.log("Inside Get Method");
let headers = new Headers({'content-type':'application/json'});
let options = new RequestOptions({ headers:this.headers });
this.responseFromFunction = this.http.get(url, options).toPromise()
.then(this.extractData)
.catch(this.handleError);
break;
}
case 'put' : {
console.log("Inside Put Method");
this.responseFromFunction = this.httpClient.put(url,data)
.subscribe((res:any) => {
console.log(res);
});
break;
}
case 'delete' : {
console.log("Inside Delete Method");
this.responseFromFunction = this.httpClient.delete(url)
.subscribe((res:any) => {
console.log(res);
});
break;
}
default : {
this.responseFromFunction = {"message":"error"};
console.log("Unknow Method Entered. Or write method in small lowercase only");
// return "Invalid Method";
}
}
console.log("Outside switch case");
console.log(this.responseFromFunction);
return this.responseFromFunction;
}
private extractData(res: Response) {
// console.log("Resp :", res.json());
// console.log("Stringy :", JSON.stringify(res));
return res.json();
}
private handleError(error: any): Promise<any> {
console.error('An error occurred', error);
return Promise.reject(error.message || error);
}
}
I am not getting how to pass username in rest_data
Upvotes: 1
Views: 1402
Reputation: 3079
IF you are using Angular 5 then :
Making a POST request
Apps often POST data to a server. They POST when submitting a form. In the following example, the HeroesService posts when adding a hero to the database. app/heroes/heroes.service.ts (addHero)
/** POST: add a new hero to the database */
addHero (hero: Hero): Observable<Hero> {
return this.http.post<Hero>(this.heroesUrl, hero, httpOptions)
.pipe(
catchError(this.handleError('addHero', hero))
);
}
The HttpClient.post()
method is similar to get() in that it has a type parameter (you're expecting the server to return the new hero) and it takes a resource URL.
It takes two more parameters:
hero - the data to POST in the body of the request.
`httpOptions` - the method options which, in this case, specify required headers.
Of course it catches errors in much the same manner described above.
The HeroesComponent
initiates the actual POST operation by subscribing to the Observable returned by this service method.
app/heroes/heroes.component.ts (addHero)
this.heroesService.addHero(newHero)
.subscribe(hero => this.heroes.push(hero));
When the server responds successfully with the newly added hero, the component adds that hero to the displayed heroes list.
Edited Answer:
I saw your postman screenshot you are passing username
Upvotes: 1