CSRF mismatch when creating item through JsonAPI
Forum rules
Always add your Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
Always add your Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
-
- Posts: 14
- Joined: 09 Jun 2021, 15:06
CSRF mismatch when creating item through JsonAPI
Hello,
our AIMEOS version is 2021.04 and we run PHP 7.4.3.
I am trying to create a product via the JsonAPI in our .NET 5 app.
I log in using the api/login endpoint and get a bearer token.
Then I use that for the OPTIONS in admin/default/jsonadm, where I get the CSRF token in the meta object.
Then I use admin/default/jsonadm/product?_token=+csrfTokenVariable.
This works alright in Postman and creates the product specified in the body parameter.
When I use this procedure in our .NET app, I can log in, get the options, but the last part where I create the product returns error 419 or whatever with the message: CSRF token mismatch.
The only thing that I suspect might be the cause of this is the Cookie that Postman creates that is really long. That is the only thing I do not pass to the product endpoint because I don't know how to generate this cookie in my program.
Thanks for any help, guys.
Jacob
our AIMEOS version is 2021.04 and we run PHP 7.4.3.
I am trying to create a product via the JsonAPI in our .NET 5 app.
I log in using the api/login endpoint and get a bearer token.
Then I use that for the OPTIONS in admin/default/jsonadm, where I get the CSRF token in the meta object.
Then I use admin/default/jsonadm/product?_token=+csrfTokenVariable.
This works alright in Postman and creates the product specified in the body parameter.
When I use this procedure in our .NET app, I can log in, get the options, but the last part where I create the product returns error 419 or whatever with the message: CSRF token mismatch.
The only thing that I suspect might be the cause of this is the Cookie that Postman creates that is really long. That is the only thing I do not pass to the product endpoint because I don't know how to generate this cookie in my program.
Thanks for any help, guys.
Jacob
Re: CSRF mismatch when creating item through JsonAPI
Yes, you need to pass the (Laravel) cookie in the request so the framework knowns the token which is stored in the user session.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos,
give us a star
If you like Aimeos,

-
- Posts: 14
- Joined: 09 Jun 2021, 15:06
Re: CSRF mismatch when creating item through JsonAPI
Thank you for the reply! How would I obtain the cookie? The login endpoint response doesn't return it.
Re: CSRF mismatch when creating item through JsonAPI
Cookies are returned as HTTP header, not in the JSON content
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos,
give us a star
If you like Aimeos,

-
- Posts: 14
- Joined: 09 Jun 2021, 15:06
Re: CSRF mismatch when creating item through JsonAPI
Hello again,
I have used the cookies from the OPTIONS call and still get the CSRF token mismatch error. Here is what I pass in parameters:

And here is the body I pass to the API:

What am I doing wrong? Thank you!
I have used the cookies from the OPTIONS call and still get the CSRF token mismatch error. Here is what I pass in parameters:

And here is the body I pass to the API:

What am I doing wrong? Thank you!
Re: CSRF mismatch when creating item through JsonAPI
The CSRF token must be passed as GET/POST parameter named "_token", not as cookie
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos,
give us a star
If you like Aimeos,

-
- Posts: 14
- Joined: 09 Jun 2021, 15:06
Re: CSRF mismatch when creating item through JsonAPI
Thanks for the repsonse! Yes, I use the token as https://url.url?_token=TOKENSTRING. But that never worked for me, so I was trying something else.
I have now disabled the csrf in the VerifyCsrfToken.php and everything works fine. I will have to re-enable csrf when we move to production though, so fingers crossed I will figure it out.