XF 2.1 Resources API

Pete Cuff

New member
Hi am running into the internal error below, even though I am specifying the username parameter: https://screencast-o-matic.com/screenshots/u/hh9M/1575889079384-12707.png . User is a superuser, posting to /api/resources/ to create a new resource. Other API calls set up and working; e.g. get resource by ID, same credentials being used. Any ideas on what else to try/set much appreciated.

---

Server error log
  • InvalidArgumentException: Attempted to convert NULL to string/binary [username]
  • src/XF/Mvc/Entity/Entity.php:740
  • Generated by: Unknown account
  • Dec 9, 2019 at 5:49 AM
Stack trace
#0 src/XF/Mvc/Entity/Entity.php(618): XF\Mvc\Entity\Entity->_castValueToType(NULL, 'username', 5, Array)
#1 src/XF/Mvc/Entity/Entity.php(548): XF\Mvc\Entity\Entity->set('username', NULL)
#2 src/addons/XFRM/Service/ResourceItem/Create.php(81): XF\Mvc\Entity\Entity->__set('username', NULL)
#3 src/addons/XFRM/Service/ResourceItem/Create.php(57): XFRM\Service\ResourceItem\Create->setupDefaults()
#4 src/XF/Container.php(270): XFRM\Service\ResourceItem\Create->__construct(Object(XF\Api\App), Object(XFRM\Entity\Category))
#5 src/XF/App.php(1570): XF\Container->createObject('XFRM\\Service\\Re...', Array)
#6 src/XF/Container.php(228): XF\App->XF\{closure}('XFRM\\Service\\Re...', Array, Object(XF\Container))
#7 src/XF/App.php(2776): XF\Container->create('service', 'XFRM:ResourceIt...', Array)
#8 [internal function]: XF\App->service('XFRM:ResourceIt...', Object(XFRM\Entity\Category))
#9 src/XF/Mvc/Controller.php(738): call_user_func_array(Array, Array)
#10 src/addons/XFRM/Api/Controller/ResourceItems.php(124): XF\Mvc\Controller->service('XFRM:ResourceIt...', Object(XFRM\Entity\Category))
#11 src/addons/XFRM/Api/Controller/ResourceItems.php(73): XFRM\Api\Controller\ResourceItems->setupResourceCreate(Object(XFRM\Entity\Category))
#12 src/XF/Mvc/Dispatcher.php(350): XFRM\Api\Controller\ResourceItems->actionPost(Object(XF\Mvc\ParameterBag))
#13 src/XF/Api/Mvc/Dispatcher.php(27): XF\Mvc\Dispatcher->dispatchClass('XFRM:ResourceIt...', 'Post', Object(XF\Api\Mvc\RouteMatch), Object(XFRM\Api\Controller\ResourceItems), NULL)
#14 src/XF/Mvc/Dispatcher.php(113): XF\Api\Mvc\Dispatcher->dispatchFromMatch(Object(XF\Api\Mvc\RouteMatch), Object(XFRM\Api\Controller\ResourceItems), NULL)
#15 src/XF/Mvc/Dispatcher.php(55): XF\Mvc\Dispatcher->dispatchLoop(Object(XF\Api\Mvc\RouteMatch))
#16 src/XF/App.php(2184): XF\Mvc\Dispatcher->run()
#17 src/XF.php(391): XF\App->run()
#18 index.php(16): XF::runApp('XF\\Api\\App')
#19 {main}

-------------

Previous InvalidArgumentException: Attempted to convert NULL to string/binary - src/XF/Mvc/Entity/ValueFormatter.php:130
#0 src/XF/Mvc/Entity/Entity.php(736): XF\Mvc\Entity\ValueFormatter->castValueToType(NULL, 5, Array)
#1 src/XF/Mvc/Entity/Entity.php(618): XF\Mvc\Entity\Entity->_castValueToType(NULL, 'username', 5, Array)
#2 src/XF/Mvc/Entity/Entity.php(548): XF\Mvc\Entity\Entity->set('username', NULL)
#3 src/addons/XFRM/Service/ResourceItem/Create.php(81): XF\Mvc\Entity\Entity->__set('username', NULL)
#4 src/addons/XFRM/Service/ResourceItem/Create.php(57): XFRM\Service\ResourceItem\Create->setupDefaults()
#5 src/XF/Container.php(270): XFRM\Service\ResourceItem\Create->__construct(Object(XF\Api\App), Object(XFRM\Entity\Category))
#6 src/XF/App.php(1570): XF\Container->createObject('XFRM\\Service\\Re...', Array)
#7 src/XF/Container.php(228): XF\App->XF\{closure}('XFRM\\Service\\Re...', Array, Object(XF\Container))
#8 src/XF/App.php(2776): XF\Container->create('service', 'XFRM:ResourceIt...', Array)
#9 [internal function]: XF\App->service('XFRM:ResourceIt...', Object(XFRM\Entity\Category))
#10 src/XF/Mvc/Controller.php(738): call_user_func_array(Array, Array)
#11 src/addons/XFRM/Api/Controller/ResourceItems.php(124): XF\Mvc\Controller->service('XFRM:ResourceIt...', Object(XFRM\Entity\Category))
#12 src/addons/XFRM/Api/Controller/ResourceItems.php(73): XFRM\Api\Controller\ResourceItems->setupResourceCreate(Object(XFRM\Entity\Category))
#13 src/XF/Mvc/Dispatcher.php(350): XFRM\Api\Controller\ResourceItems->actionPost(Object(XF\Mvc\ParameterBag))
#14 src/XF/Api/Mvc/Dispatcher.php(27): XF\Mvc\Dispatcher->dispatchClass('XFRM:ResourceIt...', 'Post', Object(XF\Api\Mvc\RouteMatch), Object(XFRM\Api\Controller\ResourceItems), NULL)
#15 src/XF/Mvc/Dispatcher.php(113): XF\Api\Mvc\Dispatcher->dispatchFromMatch(Object(XF\Api\Mvc\RouteMatch), Object(XFRM\Api\Controller\ResourceItems), NULL)
#16 src/XF/Mvc/Dispatcher.php(55): XF\Mvc\Dispatcher->dispatchLoop(Object(XF\Api\Mvc\RouteMatch))
#17 src/XF/App.php(2184): XF\Mvc\Dispatcher->run()
#18 src/XF.php(391): XF\App->run()
#19 index.php(16): XF::runApp('XF\\Api\\App')
#20 {main}
 
Last edited:

Lukas W.

Formerly katsulynx
You have to append the users ID as which you wish to post the resource as header to the request.
 

Pete Cuff

New member
@Lukas W. thanks for the pointer but sadly still no joy. Have added/tried all the combinations I can think of - user_id on it's own, username on it's own, and both as below but still the same error and the same error log. Can you see what I'm missing/getting wrong here?

Header: https://screencast-o-matic.com/screenshots/u/hh9M/1576526265798-37184.png
Params: https://screencast-o-matic.com/screenshots/u/hh9M/1576526414590-81748.png

Server error log
  • InvalidArgumentException: Attempted to convert NULL to string/binary [username]
  • src/XF/Mvc/Entity/Entity.php:740
  • Generated by: Unknown account
  • Dec 16, 2019 at 2:58 PM
Stack trace
#0 src/XF/Mvc/Entity/Entity.php(618): XF\Mvc\Entity\Entity->_castValueToType(NULL, 'username', 5, Array)
#1 src/XF/Mvc/Entity/Entity.php(548): XF\Mvc\Entity\Entity->set('username', NULL)
#2 src/addons/XFRM/Service/ResourceItem/Create.php(81): XF\Mvc\Entity\Entity->__set('username', NULL)
#3 src/addons/XFRM/Service/ResourceItem/Create.php(57): XFRM\Service\ResourceItem\Create->setupDefaults()
#4 src/XF/Container.php(270): XFRM\Service\ResourceItem\Create->__construct(Object(XF\Api\App), Object(XFRM\Entity\Category))
#5 src/XF/App.php(1570): XF\Container->createObject('XFRM\\Service\\Re...', Array)
#6 src/XF/Container.php(228): XF\App->XF\{closure}('XFRM\\Service\\Re...', Array, Object(XF\Container))
#7 src/XF/App.php(2776): XF\Container->create('service', 'XFRM:ResourceIt...', Array)
#8 [internal function]: XF\App->service('XFRM:ResourceIt...', Object(XFRM\Entity\Category))
#9 src/XF/Mvc/Controller.php(738): call_user_func_array(Array, Array)
#10 src/addons/XFRM/Api/Controller/ResourceItems.php(124): XF\Mvc\Controller->service('XFRM:ResourceIt...', Object(XFRM\Entity\Category))
#11 src/addons/XFRM/Api/Controller/ResourceItems.php(73): XFRM\Api\Controller\ResourceItems->setupResourceCreate(Object(XFRM\Entity\Category))
#12 src/XF/Mvc/Dispatcher.php(350): XFRM\Api\Controller\ResourceItems->actionPost(Object(XF\Mvc\ParameterBag))
#13 src/XF/Api/Mvc/Dispatcher.php(27): XF\Mvc\Dispatcher->dispatchClass('XFRM:ResourceIt...', 'Post', Object(XF\Api\Mvc\RouteMatch), Object(XFRM\Api\Controller\ResourceItems), NULL)
#14 src/XF/Mvc/Dispatcher.php(113): XF\Api\Mvc\Dispatcher->dispatchFromMatch(Object(XF\Api\Mvc\RouteMatch), Object(XFRM\Api\Controller\ResourceItems), NULL)
#15 src/XF/Mvc/Dispatcher.php(55): XF\Mvc\Dispatcher->dispatchLoop(Object(XF\Api\Mvc\RouteMatch))
#16 src/XF/App.php(2184): XF\Mvc\Dispatcher->run()
#17 src/XF.php(391): XF\App->run()
#18 index.php(16): XF::runApp('XF\\Api\\App')
#19 {main}

-------------

Previous InvalidArgumentException: Attempted to convert NULL to string/binary - src/XF/Mvc/Entity/ValueFormatter.php:130
#0 src/XF/Mvc/Entity/Entity.php(736): XF\Mvc\Entity\ValueFormatter->castValueToType(NULL, 5, Array)
#1 src/XF/Mvc/Entity/Entity.php(618): XF\Mvc\Entity\Entity->_castValueToType(NULL, 'username', 5, Array)
#2 src/XF/Mvc/Entity/Entity.php(548): XF\Mvc\Entity\Entity->set('username', NULL)
#3 src/addons/XFRM/Service/ResourceItem/Create.php(81): XF\Mvc\Entity\Entity->__set('username', NULL)
#4 src/addons/XFRM/Service/ResourceItem/Create.php(57): XFRM\Service\ResourceItem\Create->setupDefaults()
#5 src/XF/Container.php(270): XFRM\Service\ResourceItem\Create->__construct(Object(XF\Api\App), Object(XFRM\Entity\Category))
#6 src/XF/App.php(1570): XF\Container->createObject('XFRM\\Service\\Re...', Array)
#7 src/XF/Container.php(228): XF\App->XF\{closure}('XFRM\\Service\\Re...', Array, Object(XF\Container))
#8 src/XF/App.php(2776): XF\Container->create('service', 'XFRM:ResourceIt...', Array)
#9 [internal function]: XF\App->service('XFRM:ResourceIt...', Object(XFRM\Entity\Category))
#10 src/XF/Mvc/Controller.php(738): call_user_func_array(Array, Array)
#11 src/addons/XFRM/Api/Controller/ResourceItems.php(124): XF\Mvc\Controller->service('XFRM:ResourceIt...', Object(XFRM\Entity\Category))
#12 src/addons/XFRM/Api/Controller/ResourceItems.php(73): XFRM\Api\Controller\ResourceItems->setupResourceCreate(Object(XFRM\Entity\Category))
#13 src/XF/Mvc/Dispatcher.php(350): XFRM\Api\Controller\ResourceItems->actionPost(Object(XF\Mvc\ParameterBag))
#14 src/XF/Api/Mvc/Dispatcher.php(27): XF\Mvc\Dispatcher->dispatchClass('XFRM:ResourceIt...', 'Post', Object(XF\Api\Mvc\RouteMatch), Object(XFRM\Api\Controller\ResourceItems), NULL)
#15 src/XF/Mvc/Dispatcher.php(113): XF\Api\Mvc\Dispatcher->dispatchFromMatch(Object(XF\Api\Mvc\RouteMatch), Object(XFRM\Api\Controller\ResourceItems), NULL)
#16 src/XF/Mvc/Dispatcher.php(55): XF\Mvc\Dispatcher->dispatchLoop(Object(XF\Api\Mvc\RouteMatch))
#17 src/XF/App.php(2184): XF\Mvc\Dispatcher->run()
#18 src/XF.php(391): XF\App->run()
#19 index.php(16): XF::runApp('XF\\Api\\App')
#20 {main}
Request state
array(4) {
["url"] => string(189) "/api/resources/?api_bypass_permissions=1&resource_category_id=4&title=Test%2016th%20Dec%202019&tag_line=What%20a%20test&description=This%20is%20a%20test%20description&resource_type=fileless"
["referrer"] => bool(false)
["_GET"] => array(6) {
["api_bypass_permissions"] => string(1) "1"
["resource_category_id"] => string(1) "4"
["title"] => string(18) "Test 16th Dec 2019"
["tag_line"] => string(11) "What a test"
["description"] => string(26) "This is a test description"
["resource_type"] => string(8) "fileless"
}
["_POST"] => array(0) {
}
}
 

Pete Cuff

New member
Cracked it: needed to add "xf_api_user" (id) to the header, as "api_bypass_permissions" = 1 in the params was not bypassing the auth.
 

Lukas W.

Formerly katsulynx
It is bypassing the auth, but it still needs a user to create the resource with, which is taken from the header.
 
Top