API problem with curl

Hello
i am trying to use
I used the recommended method

curl --cookie-jar farmOS-cookie.txt -d ‘name=[USER]&pass=[PASS]&form_id=user_login’ [URL]/user/login
TOKEN=“$(curl --cookie farmOS-cookie.txt [URL]/restws/session/token)”

result:

% Total % Received % Xferd Average Dload Speed Upload
Time Total Time Spent Time Left Current Speed
100 43 100 43 0 0 38 0 0:00:01 0:00:01 --:–:–
38

then I try

curl --cookie farmOS-cookie.txt -H ‘Content-Type: application/json’ -d ‘{“name”: “Test observation via REST”, “type”: “farm_observation”, “timestamp”: “1526584271”}’ “X-CSRF-Token: ${TOKEN}”

result:

curl: (3) Illegal port number

where am i wrong?

What I would like to do is use curl inside gthumb to load observations from the photos, taking the GPS of the photo as geometry, and then speed up the loading of my observations.

Thank you

This command is incomplete. It is missing -X POST and the URL to post to (assuming you are trying to create a new log).

See the example here: API | farmOS

Hope that helps!

I try now with

curl -X POST --cookie farmOS-cookie.txt -H “X-CSRF-Token: ${TOKEN}” -d ‘{“name”: “Test observation via REST”, “type”: “farm_observation”, “timestamp”: “1526584271”}’ Farmier | farmOS Hosting, Support, and Development

and also with

curl -X POST --cookie farmOS-cookie.txt -H ‘Content-Type: application/json’ -d ‘{“name”: “Test observation via REST”, “type”: “farm_observation”, “timestamp”: “1526584271”}’ Farmier | farmOS Hosting, Support, and Development

Nothing!
I don’t understand!
I have the result terminal save in my computer
Maybe this kind of problem is to post in github? I always get confused

Is your farmOS installed in a farm subdirectory in the webroot?

If not, then your URL is wrong. For creating logs, it needs to be [URL]/log. (Not /farm/logs).

Hello and thanks!
I don’t know. It is in the farmier cloud.
For the first with -H …TOKEN
the result with /log (https://bonushenricus.farmos.net/log) is
“The page request “/log” doesn’t found”
In the case of the try with -H … .json and without TOKEN
the result is “403 Access Denied: CSRF validation failed”

You need to POST with both the cookie and the CSRF token to /log. I think that if you do a GET request to /log it will say 404 not found.

Your command should look like this exactly (I broke it up over multiple lines to make it easier to read too):

curl -X POST \
  --cookie farmOS-cookie.txt \
  -H "X-CSRF-Token: ${TOKEN}" \
  -H 'Content-Type: application/json' \
  -d '{"name": "Test observation via REST", "type": "farm_observation", "timestamp": "1526584271"}' \
  https://bonushenricus.farmos.net/log

That is assuming that your farmOS-cookie.txt file is in the same directory (from earlier), and that echo $TOKEN prints a valid token. If not, then perform the authentication steps again before running the command above (just replace [USER] and [PASS]):

curl --cookie-jar farmOS-cookie.txt -d 'name=[USER]&pass=[PASS]&form_id=user_login' https://bonushenricus.farmos.net/user/login
TOKEN="$(curl --cookie farmOS-cookie.txt https://bonushenricus.farmos.net/restws/session/token)"

I feel a little ignorant
With

curl --cookie-jar farmOS-cookie.txt \
-d ‘name=enricogabrielli76.peragr@gmail.com&pass=***********&form_id=user_login’ \
Farmier | farmOS Hosting, Support, and Development
TOKEN=“$(curl --cookie farmOS-cookie.txt \ Farmier | farmOS Hosting, Support, and Development)”
curl -X POST --cookie farmOS-cookie.txt -H “X-CSRF-Token: ${TOKEN}” \
-H ‘Content-Type: application/json’ \
-d ‘{“name”: “Test observation via REST”, “type”: “farm_observation”, “timestamp”: “1588093459”,}’ \
Farmier | farmOS Hosting, Support, and Development

(I used a timestamp for today)
there seem to be no errors, the cookie.txt is created
but no trace of the log on the web

Hmm, and are you seeing any output in the terminal after the POST? Normally it will return JSON with the ID of the new log that was created, when successful.

Don’t worry! Running curl commands can be very finicky. You have to get it perfect in order for it to work. :slight_smile:

Some thoughts:

  • That chunk of code you pasted should actually be three separate commands (maybe they got munged together and you tried to run them together?)
  • I also notice that your single and double quotes are formatted (as opening and closing quotes), rather than the plain quote characters from the keyboard. That may have been a result of copying and pasting from this forum. Just make sure that they are plain (not “left” or “right” quotes) in the commands you run.
  • You were also missing a $ in front of {TOKEN}.

I fixed all of those in the commands below. Try running these one after another:

1: Login and get cookie

curl --cookie-jar farmOS-cookie.txt -d 'name=enricogabrielli76.peragr@gmail.com&pass=***********&form_id=user_login' https://bonushenricus.farmos.net/user/login

2: Get the CSRF Token

TOKEN="$(curl --cookie farmOS-cookie.txt https://bonushenricus.farmos.net/restws/session/token)"

3: Create the log

curl -X POST --cookie farmOS-cookie.txt -H "X-CSRF-Token: ${TOKEN}" -H 'Content-Type: application/json' -d '{"name": "Test observation via REST", "type": "farm_observation", "timestamp": "1588093459"}' https://bonushenricus.farmos.net/log

Thanks but it doesn’t run
The result is:

403 Access Denied: CSRF validation failed

The token here Farmier | farmOS Hosting, Support, and Development is always the same: is it correct?
The farmOS-cookie.txt has the last line:

#HttpOnly_.bonushenricus.farmos.net TRUE / FALSE 1590133289 SESSXXXXXXXXXXXXXXXXXXXXX **********-******************************-*

Is it correct?

I try with farmOS-cookie.txt modified removing the # and changing the timestamp expire. I command new TOKEN. The TOKEN in Farmier | farmOS Hosting, Support, and Development change.
The result of curl -X POST is:

403 Forbidden

P.S.: yes double quotes are formatted wrong from a result of copying and pasting to this forum.

@bonushenricus Make sure to invalidate that session ASAP - ideally before posting it to a public forum - since it could be trivially used by a malicious third party to gain access to your farmOS instance. See https://drupal.stackexchange.com/questions/239396/how-to-secure-session-cookies

Thank you @Symbioquine - you beat me to it. :slight_smile:

Definitely need to be careful posting those details anywhere public @bonushenricus - as they would allow anyone access to your farmOS. The easiest way to invalidate them is to go to your farmOS and log out then log back in. That should reset any sessions for your user account.

I’m sorry
I believed that not writing the session and my password was enough
however I disconnected and reconnected.

@bonushenricus - Getting back to your original issue…

I just noticed another missing $ in the TOKEN= command I provided above. Sorry about that. I have edited my comment to include it.

Give those three commands another try. Fingers crossed that it works for you this time! Make sure that they are copied EXACTLY to your command prompt.

Yes!!!
You are fantastic!!!

1 Like

When I go to the countryside for my field technician job I often make visual observations with photos with my Olympus TG-3 with GPS: excellent photos.
When I get home I download them with GThumb, I select them, I make them smaller.
Often on the field I don’t have time to write many notes, also I don’t have a smartphone, the tablet is old and uncomfortable to carry around.
Here I was born the idea of creating observations on FarmOS from the photos.
This is my solution:
I create a little script to create a .json file from a photo with command

exiftool -p json.fmt -d %s file.jpeg > out.json

The json.fmt:

{
“name”: “$description#”,
“notes”:{“value”:“\u003Cp\u003E$filename#\u003C/p\u003E\n”,“format”:“farm_format”},
“type”: “farm_observation”,
“timestamp”: “$datetimeoriginal”,
“geofield”:[{“geom”:“POINT($gpslongitude# $gpslatitude#)”}]
}

For the file image it’s difficult to realise base64 I believe.
But I can upload the photo later manually. The important thing is to upload an observation that has the exact point of the photo and the exact date and time of the shot, and the description I gave to the photo.

when it comes to photos of insects I load the photos into the INATURALIST: in this way I have help in identifying.
The next step could be to use the INaturalist API to create an observation in FarmOS with the position, description and in the notes the link to the observation on INaturalist.

I’m so happy!

2 Likes

@bonushenricus great idea!! That’s a clever way of creating the JSON file with image metadata.

You inspired me to create a Python script to do this, too :smiley:

This creates an observation log with the image coordinates and timestamp. Also uploads the image

1 Like