0

I have a webservice (Actually generated with the drupal 7 plugin Service) accepting requests on 'http://mywebsite:8080/api_name/user' and checking possible connections against an 'api-key' variable in the requests, containing sort of a password.

Trying to connect to the service with my ionic2/cordova app result in a fail for the payload of the request results empty on the server side ($_REQUEST is an empty array and does not contain the api-key required, so that the request fails).

Debugging the request with https://requestb.in/ shows that the variable is in the body of the request, as expected.

The webservice is working well, on its side, for a post request sent by Google Postman go on successfully (thought I noticed a difference sending the postman request to requestb.in: 'api-key' parameter is not in the body, but in a part called "Form/Post Parameters" and the Content type is 'multipart/form-data' instead of application/json)

I am a bit confused about how my code (that seems correct) is not working...

Here is the code of the function trying to connect to the webservice:

register(username,password,email){ 
      var headers = new Headers();
      headers.append('Accept', 'application/json');
      let options = new RequestOptions({ headers: headers });

      let postParams = {
        "api-key" : "someSecret",
        "name" : "something",
        "mail" : "somethingelse"
      }

      this.http.post("http://mywebsite:8080/api_name/user", postParams, options).subscribe(data => {
          console.log("ok");
        }, error => {
          console.log("error:"+error);// Error getting the data
      });
}

EDIT: i'm reading that "$_REQUEST is used to collect data after submitting an HTML form" so the problem lays actually in sending the request as a 'multipart/form-data' but how I can do that? Changing the 'Content-type' in the header seems not to be enough. I tried adding this (with no fortune)

headers.append('Content-type', 'multipart/form-data');

1 Answer 1

1

After hours of attempts, here it is the correct code:

register(username,password,email){ 
      var headers = new Headers();
      headers.append('enctype', 'multipart/form-data');
      let options = new RequestOptions({ headers: headers });

      let postParams:FormData = new FormData();
      postParams.append("api-key", "something secret");

      this.http.post("http://mywebsite:8080/api_name/user", postParams, options).subscribe(data => {
          console.log("ok");
        }, error => {
          console.log("error:"+error); // Error getting the data
      });
}
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.