Jump to content
dragan

JSON POST woes

Recommended Posts

I'm puzzled by something I thought would be rather easy:

I want to send a request to a PW page. It's POST, and I define a header, and send data as JSON.

I need to send data in the following format:

$ POST https://mysite.com/foo/bar/ {"headers": {"Authorization": "Bearer API_KEY", "Content-Type": "application/json"}, "body": {"fields": {"Name": "<get name>", "Department": "<get team>", "Home Address": "<get address>", "Phone #": "<get phone>", "Personal Email Address": "<get email>", "Birthday": "<get birthday>", "Date Added": "<call>currentDate</call>" }}}

This is supposed to be sent via a Chatbot engine (Dexter).

In the PW page that should handle this, $_POST is always empty, as is $_REQUEST. Same for PW's $input or if ($config->ajax) {}.

I get the header, but no data.

So I dug deeper, and tried this:
@ini_set("allow_url_fopen", true);
@ini_set("always_populate_raw_post_data", true);

$data = json_decode(file_get_contents('php://input'), true);
$d    = print_r($data, true); // I store this, along with the header infos and timestamp in a PW page-field (instead of using file_put_content)

I checked page permissions, I made sure I use pagename/, i.e. with trailing slash only, to avoid stripping the header away due to redirects (which somebody in an older forum thread once highly suggested).

I tried to send the same stuff that the chatbot does via CURL. Nothing. PW error logs don't report anything (site is still in dev-mode).

PW 3.0.81 - everything else runs just fine.

Any ideas what I should change? Any more PHP/Apache settings maybe?

Help is highly appreciated.
 

Share this post


Link to post
Share on other sites

The Authorization Header is eaten up by Apache in some configurations,
for avoiding that is recommended that you send another header like

X-Auth-Token

with the same info


"Authorization": "Bearer API_KEY"
"X-Auth-Token": "Bearer API_KEY"

Then you just check if the auth is empty then fetch the other header as a fallback.

Hope it helps :)

  • Like 1

Share this post


Link to post
Share on other sites

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Mithlesh
      Changing it to null
    • By Mithlesh
      Hi, I have one URL - writerrelocations.com/contact-now/

      I have one issue where my header Image is appearing again after the contact form, for your reference:



      I have checked my processwire template section where have not added any Image field besides Header Image, attaching the same for the reference:




      PLEASE help me to get rid of the image below contact form

      Waiting for the swift response!

      Thanks
       
    • By dimitrios
      Hello,
      this module can publish content of a Processwire page on a Facebook page, triggered by saving the Processwire page.
      To set it up, configure the module with a Facebook app ID, secret and a Page ID. Following is additional configuration on Facebook for developers:
      Minimum Required Facebook App configuration:
      on Settings -> Basics, provide the App Domains, provide the Site URL, on Settings -> Advanced, set the API version (has been tested up to v3.3), add Product: Facebook Login, on Facebook Login -> Settings, set Client OAuth Login: Yes, set Web OAuth Login: Yes, set Enforce HTTPS: Yes, add "http://www.example.com/processwire/page/" to field Valid OAuth Redirect URIs. This module is configurable as follows:
      Templates: posts can take place only for pages with the defined templates. On/Off switch: specify a checkbox field that will not allow the post if checked. Specify a message and/or an image for the post.
      Usage
      edit the desired PW page and save; it will post right after the initial Facebook log in and permission granting. After that, an access token is kept.
       
      Download
      PW module directory: http://modules.processwire.com/modules/auto-fb-post/ Github: https://github.com/kastrind/AutoFbPost   Note: Facebook SDK for PHP is utilized.


×
×
  • Create New...