user39063
user39063

Reputation: 315

No data in params on POST request

Currently I have a problem with an Angular2 and RoR5 API. I'm trying to save an object to the API Server, but this doesn't work.

Here is my Angular2 Code for the service:

createFrame(frameData) {
   let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
   let options = new RequestOptions({ headers: headers });
   let body = JSON.stringify({frame: frameData});
   return this.http.post(this.frameURL, body, headers).map((res: Response) => res.json());
}

The request is made by my browser and I can see the JSON Data: enter image description here

But RoR is not able to get the POST Data.

[1] pry(#<Api::V1::FramesController>)> params
=> <ActionController::Parameters {"format"=>:json, "controller"=>"api/v1/frames", "action"=>"create"} permitted: false>
[2] pry(#<Api::V1::FramesController>)>

Can someone tell me, what's wrong?

Upvotes: 2

Views: 1603

Answers (2)

Roberto Capelo
Roberto Capelo

Reputation: 79

The problem might be related to CORS (Cross Origin Resource Sharing)

Thy to enable in your application --> Enabling cors Rails5

Good luck

Upvotes: 0

Thierry Templier
Thierry Templier

Reputation: 202156

In your case, you set the content type for form data and you send some content with the JSON format.

Either you use send form data or JSON content.

  • Form data

    createFrame(frameData) {
      let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
      let options = new RequestOptions({ headers: headers });
      let form = new URLSearchParams();
      form.set('param1', 'some value');
      let body = form.toString();
      return this.http.post(this.frameURL, body, headers).map((res: Response) => res.json());
    }
    
  • JSON data

    createFrame(frameData) {
      let headers = new Headers({ 'Content-Type': 'application/json' });
      let options = new RequestOptions({ headers: headers });
      let form = new URLSearchParams();
      form.set('param1', 'some value');
      let body = form.toString();
      return this.http.post(this.frameURL, body, headers).map((res: Response) => res.json());
    }
    

Upvotes: 2

Related Questions