<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Christian Walter</title>
    <description>The latest articles on DEV Community by Christian Walter (@itsclx).</description>
    <link>https://dev.to/itsclx</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F935123%2F506c78ea-34cb-41ed-8b22-a2685a1d7ecb.JPEG</url>
      <title>DEV Community: Christian Walter</title>
      <link>https://dev.to/itsclx</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/itsclx"/>
    <language>en</language>
    <item>
      <title>React - axios Post response has no Cookie</title>
      <dc:creator>Christian Walter</dc:creator>
      <pubDate>Mon, 10 Oct 2022 05:40:59 +0000</pubDate>
      <link>https://dev.to/itsclx/react-axios-post-response-has-no-cookie-2000</link>
      <guid>https://dev.to/itsclx/react-axios-post-response-has-no-cookie-2000</guid>
      <description>&lt;p&gt;Hello everyone,&lt;br&gt;
I am currently working with React for the frontend and Symfony PHP framework for the backend. Now I am trying to log in to the backend via React with a post request. This all works very well so far. I get a 204 code and I am then logged in. About Postman, with the same request, I get as a response a cookie for the session-based authentication. About PHP and cURL I got in the response from the backend a cookie&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8hli4axocezhdk9vpxag.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8hli4axocezhdk9vpxag.png" alt="Postman response"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The response by cURL looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{'Date': 'Sat, 08 Oct 2022 10:14:45 GMT', 'Server': 'Apache', 'Cache-Control': 'max-age=0, must-revalidate, private', 'Link': '&amp;lt;http://127.0.0.1:8000/api/docs.jsonld&amp;gt;; rel="http://www.w3.org/ns/hydra/core#apiDocumentation"', 'X-Debug-Token': 'cca986', 'X-Debug-Token-Link': 'http://127.0.0.1:8000/_profiler/cca986', 'X-Robots-Tag': 'noindex', 'Expires': 'Sat, 08 Oct 2022 10:14:45 GMT', 'Content-Encoding': 'gzip', 'Vary': 'Accept-Encoding', 'Set-Cookie': 'PHPSESSID=2606e8ab1c7ded0d30bb61890f4a6ea0; path=/; secure; httponly; samesite=lax', 'Location': '/api/users/11', 'Keep-Alive': 'timeout=5, max=100', 'Connection': 'Keep-Alive'}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;PHP Code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL =&amp;gt; 'http://127.0.0.1:8000/login',
    CURLOPT_RETURNTRANSFER =&amp;gt; true,
    CURLOPT_HEADER =&amp;gt; true,
    CURLOPT_FOLLOWLOCATION =&amp;gt; true,
    CURLOPT_HTTP_VERSION =&amp;gt; CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST =&amp;gt; 'POST',
    CURLOPT_POSTFIELDS =&amp;gt;'{
        "email": "webmaster",
        "password": "123456"
    }',
    CURLOPT_HTTPHEADER =&amp;gt; array(
        'Content-Type: application/json',
    ),
));
$response = curl_exec($curl);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As soon as I don't use in the cURL variant the option &lt;code&gt;"CURLOPT_HEADER =&amp;gt; true&lt;/code&gt;," the cookie and the detailed response are no longer displayed.&lt;/p&gt;

&lt;p&gt;Now I have tried all options to get the cookie also in React and Axios in the header, unfortunately it is not possible for me even remotely somehow to get a cookie or other response from the backend. Here is the code I am currently using.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let data = {email:"webmaster", password:"123456"}
let headers = {
  headers: {'Content-Type': 'application/json'}
};


const axiosConfig = {
  headers: {
    'content-Type': 'application/json',
    "Accept": "/",
    "Cache-Control": "no-cache",
    "Access-Control-Allow-Origin": "*",
    // "Cookie": document.cookie
  },
  credentials: "same-origin"
};
axios.defaults.withCredentials = true;
const res = await axios.post('http://127.0.0.1:8000/login', data, axiosConfig)
    .catch(response =&amp;gt;{
      console.log(response.getAll());
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs9b6jtye8y5xn2m1d4hn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs9b6jtye8y5xn2m1d4hn.png" alt="React response after Post request"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Unfortunately I don't know if I forgot anything in the post request or if Axios is not the best for such a request.&lt;/p&gt;

&lt;p&gt;I am just surprised that in Postman and via PHP there are no problems to get a cookie. Unfortunately, I have only recently started working with ReactJS and have very little experience in this area.&lt;/p&gt;

&lt;p&gt;On the weekend I tried it also with python and there were also no problem. So I think it is a problem of my ReactJS code.&lt;/p&gt;

&lt;p&gt;Thanks a lot for your help!&lt;/p&gt;

&lt;p&gt;Best, Christian&lt;/p&gt;

</description>
      <category>react</category>
      <category>symfony</category>
      <category>php</category>
      <category>axios</category>
    </item>
  </channel>
</rss>
