× Requests and support related to jBackend.

jBackend POST API KEY

More
5 years 8 months ago #3502 by admin
Replied by admin on topic jBackend POST
Thanks to you for using jBackend. :)

Luigi

Please Log in or Create an account to join the conversation.

  • AndyG
  • Topic Author
  • Offline
  • Fresh Boarder
  • Fresh Boarder
More
5 years 8 months ago #3504 by AndyG
Replied by AndyG on topic jBackend POST
Hi Luigi

We did hit a JSON issue with post. Not really due to jBackend, but thought I'd jot them here for future googlers and other jBackend users.

The JSON POST was not getting through, which seemed to be a a know issue.

So, the normal Joomla way of getting the data, like...
$fields = $app->input->get('fields', null, 'array');
...but we switched to...
$rest_json = file_get_contents("php://input");
$_POST = json_decode($rest_json, true);    
$fields = $_POST['fields'];
...and that worked well.

I'm a programmer and web designer to Software Systems: Open For Business in Aberdeen UK. We do web dev, mostly with Joomla.

Please Log in or Create an account to join the conversation.

More
5 years 7 months ago #3507 by admin
Replied by admin on topic jBackend POST
Thanks for reporting here. :)

Just last note for readers... it is also important to set the proper header in the POST request:
Content-Type: application/json

This makes request compliant to specifications, and is also necessary for php://input to work:

php://input is not available with enctype="multipart/form-data".


More information here:

php.net/manual/it/wrappers.php.php

Kind regards,
Luigi

Please Log in or Create an account to join the conversation.

  • pwalters@giantbrain.net
  • Offline
  • Fresh Boarder
  • Fresh Boarder
More
1 year 3 months ago #7091 by pwalters@giantbrain.net
Replied by pwalters@giantbrain.net on topic jBackend POST API KEY
In the docs, it specifies using the 'Authorization' header to embed the API key as this:
Authorization: api_key LSOSFOFJOWOIEFJ9M7YA

However, in my development I found that I was never receiving the API KEY.

I looked into it and found that in the jBackend Module Request (where the api_key is getting parsed) the "Authorization" header was not found in the incoming headers and therefore the API KEY was never found.

I verified this by adding an "error_log( var_dump($_SERVER)); and then looking at all fields within the $_SERVER. The "Authorization" header was not there.

So I added the following to implement a more standard way of handling the API Key.
file: com_jbackend/models/request.php
//   This is the original code. The "Authorization" header was never found here.
        if (isset($_SERVER['HTTP_AUTHORIZATION']))
        {
          $header_auth = explode(' ', $_SERVER['HTTP_AUTHORIZATION']);

          if ($header_auth[0] === 'api_key')
              $api_key = $header_auth[1];
        }
 // Note: My addition is here:
        else  //<--- this is the more STANDARD  way to handle the API KEY.
        {
          // Check API key the standard  way ... using the new standard API-KEY
          if( isset($_SERVER['HTTP_API_KEY']) )
          {
            $header_auth = explode(' ', $_SERVER['HTTP_API_KEY']);
            $api_key =  $header_auth[0];
          }
        }

So now I use the following header to pass in the API KEY and all is good!
Api-Key: LSOSFOFJOWOIEFJ9M7YA

BTW: Thanks for a GREAT product. Your jBackend has made my development SO much easier! I would highly recommend it!

Please Log in or Create an account to join the conversation.

Time to create page: 0.145 seconds