-1

My script that shall do the work:

updateTask(task:Task){
    const body = task;
    var headers = new HttpHeaders();
    headers.set('Content-Type', 'application/javascript');
    headers.set('If-Match', task.etag);
    return this.httpc.put(this.resources+'/'+task.id, body, { headers: headers });
}

Could anyone take a look at it and tell me if I did something wrong here please? Never tried it before, actually I am beginner to angular :S ( Back-end is not mine so I can't share with you guys :/ )

httpc is a HttpClient :) Thanks your time and answers in advance! ^^.

Edit 1:

Response > {"error":"If-Match header is missing or invalid"}

Edit2:

Request header

PUT /app/data/schedule/tasks/42615 HTTP/1.1
Host: localhost:3000
Connection: keep-alive
Content-Length: 438
Accept: application/json, text/plain, */*
Origin: http://localhost:3000
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36
Content-Type: application/json
Referer: http://localhost:3000/portal/
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Cookie: sid=f5eb691a56bfa7b6db05083023c5f4caf044db47c2f29f644bc80d7bb7dcf1f3
9
  • What is the error? What is this.resources? What contains Task? Is javascript script? It is difficult to help you if we do not have more information. you should think to provide a Minimal, Complete, and Verifiable example. Also try to test your query (in this case put) using a rest client in your browser. To be sure, for example, if the URL is well formatted Commented Dec 8, 2017 at 13:01
  • I have already tested it without angular. I am just not sure where to set the If-Match part. This is a function of a service but it still does not matter because the main thing is not about what is Task or if the url is correct. I could put a simple number so we could say number insted of Task but this wont change the problem itself. I guess I didnt set the If-Match part correctly. (task.etag is a simple etag string like: "3a62ca468a0c07d6040b30a5a263a19a"). Sorry if I am not clear with this :| Just didn't found any Q or example about put with angular when you need to set the If-Match field. Commented Dec 8, 2017 at 13:43
  • Your problem is related to task.etag, because the value is null or is invalid. Check if the header If-Match is well formatted and has a proper value. Commented Dec 8, 2017 at 14:00
  • It has the value I copy pasted into brackets. I copied the request header from chrome but it shows that my header didn't changed at all. Commented Dec 8, 2017 at 14:03
  • But. Where is the header If-Match, this header is mandatory according to the error Commented Dec 8, 2017 at 14:06

1 Answer 1

2

Summarizing the great amount of comments to solve this question. The backend does not accept an HTTP PUT command because the headers are not properly setting. So, to fix this problem, you should add both headers properly in your code. There is several ways. I proposed one, based on constructor, but there is more.

Based on constructor:

const header = 
new HttpHeaders({'Content-Type': 'application/javascript', 'If-Match' : task.etag});

Using append method (as this question suggests):

const headers = new HttpHeaders();
headers.append('Content-Type', 'application/javascript');
headers.append('If-Match', task.etag);

Note the change made in the declaration of header. I propose const instead var.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.