Skip to content

Instantly share code, notes, and snippets.

@larseirik
Forked from arfaram/ezrest_curl_httpie.md
Created November 27, 2017 07:51
Show Gist options
  • Save larseirik/80256ce6488a7fd572e92d895cc91a85 to your computer and use it in GitHub Desktop.
Save larseirik/80256ce6488a7fd572e92d895cc91a85 to your computer and use it in GitHub Desktop.
Command line using curl and http with eZPlatform Rest API v2

root Location information

curl -u "admin:publish" -H "Accept: application/vnd.ez.api.Root+xml" http://cologne.tripandtravel.dev/api/ezp/v2/

curl -u admin -H "Accept: application/vnd.ez.api.Root+json" http://cologne.tripandtravel.dev/api/ezp/v2/
http -a "admin:publish" --json http://cologne.tripandtravel.dev/api/ezp/v2/

location info

curl -u "admin:publish" -H "Accept: application/vnd.ez.api.Location+xml" http://cologne.tripandtravel.dev/api/ezp/v2/content/locations/1/2/94/95/99

or using : -H "Accept: application/vnd.ez.api.ContentInfo+xml"

load location children

curl -u "admin:publish" -H "Accept: application/vnd.ez.api.LocationList+xml" http://cologne.tripandtravel.dev/api/ezp/v2/con
ations/1/2/94/95/99/children

load content

curl -u "admin:publish" -H "Accept: application/vnd.ez.api.Content+xml" http://cologne.tripandtravel.dev/api/ezp/v2/content/objects/100

Get CSRF Token for authentication

curl -i  -X POST -d @rest_auth.json -H "Content-Type: application/vnd.ez.api.SessionInput+json" -H "Accept: application/vnd.ez.api.Session+json" http://cologne.tripandtravel.prod/api/ezp/v2/user/sessions

rest_auth.json:

{"SessionInput": {"login": "admin","password": "publish"}}

Create content

Create & publish an image using the eZ Platform REST API v2

contentType:5 (image)

section:3 (media)

location:/1/43/51 (Default: Media->Images)

Content Language :ger-DE

The image's content is provided as base64:

openssl enc -base64 -in image.jpeg > image.b64

Publishing steps:

  1. create a Draft
  2. get the draft content id
  3. publish the draft using the content id

Below is the image content in json:

{
  "ContentCreate": {
    "ContentType": {
      "_href": "/api/ezp/v2/content/types/5"
    },
    "mainLanguageCode": "ger-DE",
    "LocationCreate": {
      "ParentLocation": {
        "_href": "/api/ezp/v2/content/locations/1/43/51"
      },
      "sortField": "PATH",
      "sortOrder": "ASC"
    },
    "Section": {
      "_href": "/api/ezp/v2/content/sections/3"
    },
    "fields": {
      "field": [
        {
          "fieldDefinitionIdentifier": "name",
          "fieldValue": "eZ Systems logo created with the REST API"
        },{
          "fieldDefinitionIdentifier": "image",
          "fieldValue": {
              "fileName": "ezlogo.gif",
              "fileSize": 622,
              "data": "R0lGODlhRQBGAIQTAAAAABEEASIJAkQTBCAgICIiIjMzM4gmCERERFVVVWRkZP9JD3d3d4iIiJmZmaqqqru7u8zMzO7u7v///////////////////////////////////////////////////yH5BAEKAB8ALAAAAABFAEYAAAX+ICKOZGmeaKqSzue+LyDPdG3feK7TDOzvwKAw1/PFhsikrmj8KJ9QGdNIa0yu2Kx2y+16tbymq/otm8/ZsJiMbrvTs+lvZn3b0eom+8735ql0fYJcf3MydYOJE4Uwe4qCjEeHj4mRY4GUkHFiTpiZfJadk5+gm2uepG+hjqltq6iteKZ6sLFmr6O2slKcrLpfuACIaBErxik0BCgMvmUQUdAyCM1fz9FR07XO19jUXtYyAgPj5OXm5+jpATPZuWjgAAcL8/T19vf4+QPs3l3w8vkCCtTHT1s1GgAHKhy4T1o/Lv8WSgzYEEA7YW8iTtxYr+LFYWc0cuTo8eEWkSPsJ5Y0+A1hSpIF3YV0+VJlTIxuUNZkeBOkGZ07KfbMSDMoT4cs/RU1KhSpzJ9LmeJb+XTbjIRSpw7NGTVrx61tgHqdRxVn2K5jF5T1aVUG1rRkwb5DO3Yt0atw7dnlijcvvb1n+/pVK3emYL+A5x7Om9iw28FxnZpV/BhyWQQNMmvezDmzArpey0Z7C1c0NNJpTUdBXffmaMiEpXGa/UJsa9m0Z9sOzS63btBZPfrmtDt47+FN4A04wLy58+fQo0sXcBy5D3jckCCwbgR7diHbucPw/h1IePEuyJfXcR79Bwjw48ufT7++/fsRQgAAOw=="
          }
        }
      ]
    }
  }
}

1.Create Draft

using curl or http

curl --user admin:publish -i -H "Accept: application/vnd.ez.api.Content+json" -H "Content-Type: application/vnd.ez.api.ContentCreate+json" -X POST -d @imagecontent.json http://cologne.tripandtravel.dev/api/ezp/v2/content/objects 

OR

cat imagecontent.json | http --print hb --auth "admin:publish" POST http://cologne.tripandtravel.dev/api/ezp/v2/content/objects 'Accept: application/vnd.ez.api.Content+json' 'Content-Type: application/vnd.ez.api.ContentCreate+json'

2. get the draft content id

see response body:

"status": "DRAFT", 
"versionNo": 1
}, 
	"_href": "/api/ezp/v2/content/objects/144/versions/1",

Draft Content id:144

3. publish the draft using the content id

using curl or http

curl --user admin:publish -i -X POST http://cologne.tripandtravel.dev/api/ezp/v2/content/objects/144/versions/1  -H 'X-HTTP-Method-Override:Publish' 

OR

http --print hb --auth "admin:publish" PUBLISH http://cologne.tripandtravel.dev/api/ezp/v2/content/objects/144/versions/1

Response OK : HTTP/1.1 204 No Content

script to publish content

# Create & publish an image using the eZ Platform REST API v2
# Notes:
# - this script requires http ie, which can be found at https://github.com/jkbr/httpie
# - Thanks to : Bertrand Dunogier <http://share.ez.no/community/profile>
# more infos: https://gist.github.com/bdunogier/3918294

REST_URI="http://cologne.tripandtravel.dev/api/ezp/v2"
EZP_USER="admin"
EZP_PASSWORD="publish"

####
echo "POST /content/objects result"

CREATE_RESULT=`cat imagecontent.json | http --print h --auth "${EZP_USER}:${EZP_PASSWORD}" POST ${REST_URI}/content/objects 'Accept: application/vnd.ez.api.Content+json' 'Content-Type: application/vnd.ez.api.ContentCreate+json'`

echo $CREATE_RESULT > log.log

HTTP_RESULT=`cat -v log.log | head -n 1 | sed 's#HTTP/1\.1 ##'`
HTTP_RESULT_CODE=`echo "$HTTP_RESULT" | egrep -o "^[0-9]+"`

if [ $HTTP_RESULT_CODE -ge 300 ]; then
    echo $HTTP_RESULT
    exit 0;
fi

CREATED_CONTENT_VERSION_ID=`cat -v log.log | grep -o -P '(?<=Location: /api/ezp/v2/content/objects/).*(?=)' | egrep -o "^[0-9]+"`
echo "Created content id: $CREATED_CONTENT_VERSION_ID"

echo "PUBLISH ${CREATED_CONTENT_VERSION_ID}/versions/1"
http --print hb --auth "${EZP_USER}:${EZP_PASSWORD}" PUBLISH ${REST_URI}/content/objects/${CREATED_CONTENT_VERSION_ID}/versions/1


echo "Do not forget to clear content cache"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment