<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: 403 error when sending json from php in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/403-error-when-sending-json-from-php/m-p/12643323#M2014432</link>
    <description>&lt;P&gt;Hi, &lt;/P&gt;&lt;P&gt;your screenshot shows in the response:&lt;/P&gt;&lt;P&gt;x-csrf-token: required&lt;/P&gt;&lt;P&gt;as Evan pointed out&lt;/P&gt;</description>
    <pubDate>Fri, 22 Jul 2022 14:27:26 GMT</pubDate>
    <dc:creator>CarlosRoggan</dc:creator>
    <dc:date>2022-07-22T14:27:26Z</dc:date>
    <item>
      <title>403 error when sending json from php</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/403-error-when-sending-json-from-php/m-p/12643319#M2014428</link>
      <description>&lt;H1&gt;How to make a request using HTTP basic authentication with PHP curl?&lt;/H1&gt;
  &lt;P&gt;I am sending a json from php to a Sap web service but it tells me a 403 forbidden error when I see the response, I tried sending the json with postman and it works&lt;/P&gt;
  &lt;P&gt;find that you have to make a get request first to get a token, and with that a post request is made to send that data&lt;/P&gt;
  &lt;P&gt;here my functions&lt;/P&gt;
  &lt;P&gt;with the first one I get a token that is the one that allows to send the data to sap and the second function sent the data is called $payload. but when printing the response, the code 403 forbidden appears&lt;/P&gt; 
  &lt;PRE&gt;&lt;CODE&gt;&amp;lt;code&amp;gt; $token = getCSRFToken($username,$password,$url);
$upload = uploadData($payload,$token['x-csrf-token'][0],$token['set-cookie'][1],$username,$password,$url);
function uploadData($json,$token,$cookie,$username,$password,$url) {
    
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array (
    'x-csrf-token: ' . $token,
    'Cookie: ' . $cookie,
    'Content-Type: application/json',
    'Content-Length: ' . strlen($json),
    'Accept: application/json'
    ));
    curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);

    $result = curl_exec($ch);
    echo $result;
    
    return curl_getinfo($ch);

}

function getCSRFToken($username,$password,$url) {

  $ch = curl_init($url);
  $request_headers = array();
  $request_headers[] = 'X-CSRF-Token: Fetch';
  $request_headers[] = 'Content-Type: application/json';
  $request_headers[] = 'Accept: application/json';
  curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
  curl_setopt($ch, CURLOPT_POST, 0);
  curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_VERBOSE, 1);
  curl_setopt($ch, CURLOPT_HEADER, 1);
  curl_setopt($ch, CURLOPT_NOBODY, true);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
  curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($curl, $header) use (&amp;amp;$headers)
  {
    $len  = strlen($header);
    $header = explode(':', $header, 2);
    if (count($header) &amp;lt; 2) { // ignore invalid headers
    return $len;
  }

  $name = strtolower(trim($header[0]));
  if (is_array($headers) &amp;amp;&amp;amp; !array_key_exists($name, $headers)) {
    $headers[$name] = [trim($header[1])];
  } else {
    $headers[$name][] = trim($header[1]);
  }
  return $len;

  });

  $tmpfname = '/tmp/cookie.dat';
  curl_setopt($ch, CURLOPT_COOKIESESSION, true);
  curl_setopt($ch, CURLOPT_COOKIEJAR, $tmpfname);
  $resp = curl_exec($ch);
  return $headers;

}&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 21 Jul 2022 16:54:53 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/403-error-when-sending-json-from-php/m-p/12643319#M2014428</guid>
      <dc:creator>former_member813802</dc:creator>
      <dc:date>2022-07-21T16:54:53Z</dc:date>
    </item>
    <item>
      <title>Re: 403 error when sending json from php</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/403-error-when-sending-json-from-php/m-p/12643320#M2014429</link>
      <description>&lt;P&gt;Welcome to the SAP Community. Thank you for visiting us to get answers to your questions.&lt;/P&gt;&lt;P&gt;Since you're asking a question here for the first time, I'd like to offer some friendly advice on how to get the most out of your community membership and experience.&lt;/P&gt;&lt;P&gt;First, please see &lt;A href="https://community.sap.com/resources/questions-and-answers" target="test_blank"&gt;https://community.sap.com/resources/questions-and-answers&lt;/A&gt;, as this resource page provides tips for preparing questions that draw responses from our members. Second, feel free to take our Q&amp;amp;A tutorial at &lt;A href="https://developers.sap.com/tutorials/community-qa.html" target="test_blank"&gt;https://developers.sap.com/tutorials/community-qa.html&lt;/A&gt;, as that will help you when submitting questions to the community.&lt;/P&gt;&lt;P&gt;I also recommend that you include a profile picture. By personalizing your profile, you encourage readers to respond: &lt;A href="https://developers.sap.com/tutorials/community-profile.html" target="test_blank"&gt;https://developers.sap.com/tutorials/community-profile.html&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;Now for some specific suggestions on how you might improve your question:&lt;/P&gt;&lt;P&gt;* Outline what steps you took to find answers (and why they weren't helpful) -- so members don't make suggestions that you've already tried.&lt;/P&gt;&lt;P&gt;* Share screenshots of what you've seen/done (if possible), as images always helps our members better understand your problem.&lt;/P&gt;&lt;P&gt;* Make sure you've applied the appropriate tags -- because if you don't apply the correct tags, the right experts won't see your question to answer it.&lt;/P&gt;&lt;P&gt;Should you wish, you can revise your question by selecting Actions, then Edit. &lt;/P&gt;&lt;P&gt;The more details you provide (in questions tagged correctly), the more likely it is that members will be able to respond. As it stands, I don't know if there is enough information here for members to understand your issue. So please consider revising your question because I'd really like to see you get a solution to your problem!&lt;/P&gt;&lt;P&gt;I hope you find this advice useful, and we're happy to have you as part of SAP Community!&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jul 2022 16:55:20 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/403-error-when-sending-json-from-php/m-p/12643320#M2014429</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2022-07-21T16:55:20Z</dc:date>
    </item>
    <item>
      <title>Re: 403 error when sending json from php</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/403-error-when-sending-json-from-php/m-p/12643321#M2014430</link>
      <description>&lt;P&gt;I haven't used PHP previously but I would suggest:&lt;/P&gt;&lt;P&gt;(1) Printing the value of $token upon entry to uploadData.&lt;/P&gt;&lt;P&gt;(2) Printing the response headers and payload for the 403 response. If the 403 response has "X-CSRF-Token: Required' that would imply that the token didn't get through properly.&lt;/P&gt;&lt;P&gt;Also I am curious that the call to uploadData has:&lt;/P&gt;&lt;P&gt;$token['x-csrf-token'][0]&lt;/P&gt;&lt;P&gt;but also:&lt;/P&gt;&lt;P&gt;$token['set-cookie'][1]&lt;/P&gt;&lt;P&gt;which has me wondering, if the response to the token fetch request returns multiple cookies, but then the uploadData call is sending only the cookie at index [1], perhaps the 403 response is due to missing cookie(s) rather than a bad or missing token.&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jul 2022 21:33:10 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/403-error-when-sending-json-from-php/m-p/12643321#M2014430</guid>
      <dc:creator>evanireland</dc:creator>
      <dc:date>2022-07-21T21:33:10Z</dc:date>
    </item>
    <item>
      <title>Re: 403 error when sending json from php</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/403-error-when-sending-json-from-php/m-p/12643322#M2014431</link>
      <description>&lt;P&gt;Aquí muestro en pantalla el token y la galleta que me trae el get, en la foto muestro los encabezados que me trae y en las siguientes muestro solo el token y la galleta&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jul 2022 13:08:20 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/403-error-when-sending-json-from-php/m-p/12643322#M2014431</guid>
      <dc:creator>former_member813802</dc:creator>
      <dc:date>2022-07-22T13:08:20Z</dc:date>
    </item>
    <item>
      <title>Re: 403 error when sending json from php</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/403-error-when-sending-json-from-php/m-p/12643323#M2014432</link>
      <description>&lt;P&gt;Hi, &lt;/P&gt;&lt;P&gt;your screenshot shows in the response:&lt;/P&gt;&lt;P&gt;x-csrf-token: required&lt;/P&gt;&lt;P&gt;as Evan pointed out&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jul 2022 14:27:26 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/403-error-when-sending-json-from-php/m-p/12643323#M2014432</guid>
      <dc:creator>CarlosRoggan</dc:creator>
      <dc:date>2022-07-22T14:27:26Z</dc:date>
    </item>
    <item>
      <title>Re: 403 error when sending json from php</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/403-error-when-sending-json-from-php/m-p/12643324#M2014433</link>
      <description>&lt;P&gt;Apparently it wasn't the token, it was the cookie apparently it's a longer string than what I'm adding&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jul 2022 17:31:19 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/403-error-when-sending-json-from-php/m-p/12643324#M2014433</guid>
      <dc:creator>former_member813802</dc:creator>
      <dc:date>2022-07-22T17:31:19Z</dc:date>
    </item>
  </channel>
</rss>

