2

I want to convert this cURL to angular 2 post request

curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: Basic cGJob2xlOmlJelVNR3o4" -H "Origin: http://localhost:4200/form" -H "Postman-Token: fbf7ede1-4648-a330-14ee-85e6c29ee80d" -d 'content=Queue: tsi-testdesk' "https://testdesk.ebi.ac.uk/REST/1.0/ticket/new?user=USER&pass=PASS"

here is the code i wrote but its not working.

 addForm(form: Form): Observable<Form> {
     console.log(" SUBMITTING FORM");
    let headers = new Headers();
    this.loginService.writeAuthToHeaders(headers);
    // JSON.stringify(headers);
    // headers.append('Accept', 'application/x-www-form-urlencoded');
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    // let text = JSON.stringify(form)
    let content = ('content:Queue: tsi-testdesk');
    console.log(content);
     return this.http.post('https://testdesk.ebi.ac.uk/REST/1.0/ticket/new?user='+this.credentialsService.getUsername()+'&pass='+this.credentialsService.getPassword(), content, { headers: headers })
      // .map(response => <Form>response.json())
      .catch(this.handleError);
   }

It is giving me pre-flight response fail error but it works fine with cURL as well as POSTMAN and also I Dont have access to server side I am contacting it through API

user5843174
  • 151
  • 1
  • 10
  • @georgeawg answer is not accepted in post neither there is enough explanation on how to solve the problem – user5843174 Dec 12 '16 at 16:00
  • Browsers block cross-origin posts to prevent evil web sites from using JavaScript on users computers to doing evil posts. Evil websites can post to themselves but not cross origin. Servers must give browsers permission to allow cross-origin posts. The server does that by indicating permission in pre-flight responses with CORS headers. Those headers need to be set up on the server side. If you don't have access to the server, solutions are limited to browser extensions or proxy servers. – georgeawg Dec 12 '16 at 16:15
  • @georgeawg ok i understand that but than how does one in curl works or postman works. I know postman is not bounded by CORS policy but you can put constraint of origin in header to make it bound and I have done that and it works fine with it – user5843174 Dec 12 '16 at 16:27
  • See [CORS issue doesn't occur when using POSTMAN](http://stackoverflow.com/questions/36554013/cors-issue-doesnt-occur-when-using-postman). – georgeawg Dec 12 '16 at 16:44

1 Answers1

0

CORS is a policy that is enforced by the web browser. Ultimately, it is up to the browser, whether or not it will allow a cross-origin request. In the case of cURL or Postman, there is no browser, there is no current HOST, so there is not even the concept of a cross-origin request. Technically Postman is a Chrome extension, but it is not at all the same thing as loading a web page and making cross-origin requests.

Public-facing API's (probably like the one you are trying to access) already have CORS enabled. The likely culprit is your own server. You must enable CORS requests on your web server so it will allow you to make requests to outside APIs.

Pop-A-Stash
  • 6,572
  • 5
  • 28
  • 54
  • I have a spring backend and hace cors enabled on it. I am trying to make POST through front end though because of some business logic issue. But i am not creating session could that be a reason as it says authorisation needs session to be created – user5843174 Dec 13 '16 at 09:02
  • And also if my post is equivalent to cURL mentioned above – user5843174 Dec 13 '16 at 09:02