Click here to Skip to main content
15,886,072 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to perform CRUD operation for rest api.

Currently I am performing the update operation for a form on button click.

Problem :

In Postman

When I hit the api by passing api key in headers section and required parameters in body section and press "Send", the call completed successfully and I am able to see the response.

In code

When I click the button , the same api gets called but I get error with the below message.

{"status":"error","error":"Please provide a task id"}

What I have tried:

My api call :

Request URL : https://devza.com/tests/tasks/update
Request headers :

authority: devza.com
:method: POST
:path: /tests/tasks/update
:scheme: https
accept: application/json
accept-encoding: gzip, deflate, br
accept-language: en-US,en;q=0.9
authtoken: MMPIUq9bM8zRUFDWzc7XMMGt8jix38q9
content-length: 104
content-type: application/json
origin: http://localhost:4200
referer: http://localhost:4200/
sec-ch-ua: "Chromium";v="86", "\"Not\\A;Brand";v="99", "Google Chrome";v="86"
sec-ch-ua-mobile: ?0
sec-fetch-dest: empty
sec-fetch-mode: cors
sec-fetch-site: cross-site
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36

Request Payload :

[{"taskid":"837","message":"stories","priority":"2","assigned_to":"1","due_date":"2020-11-08 12:12:12"}]

Response : {"status":"error","error":"Please provide a task id"}

As per the response , it asks me for a task id , but it is already provided in the payload section.
Posted
Comments
Richard MacCutchan 2-Nov-20 7:16am    
You need to talk to the provider of the API to see why it does not accept the taskid value.
AmitabhaGhosh123 2-Nov-20 9:30am    
Hi Richard,

The same request is working fine in postman but not working while calling the api from code.

Below is the documentation for the update api.

POST Update Tasks
https://devza.com/tests/tasks/update
HEADERS

AuthToken {YOURTOKENHERE}

BODY formdata

message Do xxx something dasasa
due_date 2020-09-19 12:12:12
optional

priority 2
optional (1: normal, 2: mid, 3: high)

assigned_to 1
optional

taskid 1
Richard MacCutchan 2-Nov-20 10:04am    
But that does not explain the format that the data must be sent in. You still need to find out why the taskid that you are sending is not being recognised by the API. It is not something that anyone here can guess.
F-ES Sitecore 2-Nov-20 8:05am    
What's the method signature of the api you're calling? Your passing an array of data rather than a single item so it might be that, without knowing how the method is defined it's impossible to say what the problem might be.
AmitabhaGhosh123 2-Nov-20 9:26am    
Below is the method for api :

updateTask(data) {
return this.http.post(this.apiBaseUrl + '/update', JSON.stringify(data),{
headers: new HttpHeaders({
"authtoken":'MMPIUq9bM8zRUFDWzc7XMMGt8jix38q9',
"content-type": 'application/json',
"accept": 'application/json'
})
})
}

Method for calling the api :

updateTask() {
this.updateObject['taskid'] = this.filteredObject['id'];
this.updateObject['message'] = this.filteredObject['message'];
this.updateObject['due_date'] = this.filteredObject['due_date'];
this.updateObject['priority'] = this.filteredObject['priority'];
this.updateObject['assigned_to'] = this.filteredObject['assigned_to'];
this.spinner.show('spinner1');
var data = [];
data.push(this.updateObject);
this.taskservice.updateTask(data).subscribe(res=>{
console.log(res);
$('#content').modal("hide");
this.spinner.hide('spinner1');
this.fetchAllTasks();
},
(error)=>{
this.spinner.hide('spinner1');
});
}


The api accepts formdata as input in the body section and apikey in headers.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900