I am trying to get the amount of reputation that a user has
how to handle the response (or get one) from a script
You can do this with a Bash command that you can then embed in a script. Here's how:
1. Look up your user ID
Assuming you want to get the reputation on Stack Overflow, go to your account and look up your user ID in the URL:
https://stackoverflow.com/users/123456/your-name
In this example, the ID is 123456.
2. Get user info
Make your first API call with curl:
curl --compressed https://api.stackexchange.com/2.2/users/123456/?site=stackoverflow
This will print a single line of JSON data about you on Stack Overflow, including your reputation.
The --compressed
flag makes curl uncompress the response which is compressed using gzip.
We can format the data using jq
to make it easier to read:
curl --compressed https://api.stackexchange.com/2.2/users/123456/?site=stackoverflow | jq
Given that you have jq
installed, this will produce much nicer output:
{
"items": [
{
"badge_counts": {
"bronze": 122,
"silver": 85,
"gold": 11
},
"account_id": 999999,
"is_employee": false,
"last_modified_date": 1560547206,
"last_access_date": 1562693872,
"reputation_change_year": 2168,
"reputation_change_quarter": 90,
"reputation_change_month": 90,
"reputation_change_week": 30,
"reputation_change_day": 0,
"reputation": 15343,
"creation_date": 1306744511,
"user_type": "registered",
"user_id": 123456,
"accept_rate": 85,
"location": "Austria",
"website_url": "https://www.you.com",
"link": "https://stackoverflow.com/users/123456/your-name",
"profile_image": "https://i.sstatic.net/SyKPw.jpg?s=128&g=1",
"display_name": "Your Name"
}
],
"has_more": false,
"quota_max": 300,
"quota_remaining": 268
}
3. Filter the data
Alright, now that we have the data, how do we get at just the user's reputation?
Using jq
's filters:
curl --compressed https://api.stackexchange.com/2.2/users/123456/?site=stackoverflow | jq '.items[0].reputation'
This will print the reputation of the user with ID 123456:
15343