CachetHQ Incident API - php

I have a basic CachetHQ status page set up, and have gone through the entire configuration process. I have my user, who has an API key.
I have been able to get the creation of an incident working through the API (authenticating and posting to http://domain.com/api/v1/incidents) however, when I try to do the PUT request with a specific incident in mind, it fails giving me a "403 - Forbidden". Is there something special which I need to do in order to support this?
The cURL request looks like this:
curl -X PUT -H "X-Cachet-Token: API-KEY-HERE" -H "Cache-Control: no-cache" -H "Postman-Token: 6e023e6d-329a-9b00-b027-33b259264680" -H "Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW" -F "name=Incident #1 - Foo" -F "component_id=3" -F "status=1" "http://status.icepets.com/api/v1/incidents/4"

Based on my previous experiences on the server, I have figured out the issue. The server was not accepting all of the different verbs (i.e. PUT/DELETE).
I have switched my script to use Laravel's spoofed method, and it worked like a charm.

Related

How to upload file in dropbox using php or curl [duplicate]

I am searching everywhere and haven't been able to locate a suitable example and am not well versed enough to be able to sort it out via the docs. Could someone with more knowledge than I show me how to form the CURL command for OAUTH 2? And is it that I only need the OAUTH 2 secret key? I am being shown an App key, app secret and oauth 2. I am using this in a perl script if it matters.
The closest code I have found is this:
curl --request PUT --header "Content-Length: `ls -la jonathan.txt | awk '{ print $5}'`" --header
"Content-Type: multipart/mixed" --data-binary "#jonathan.txt" "https://api-
content.dropbox.com/1/files_put/dropbox/jonathan.txt?access_token=ABCDEF"
But I don't think that is OAUTH 2?
If you have an access token (created via the app console):
curl -H "Authorization: Bearer <your token>" https://api-content.dropbox.com/1/files_put/auto/ -T <your file path>
You need an access token for the account. (This is typically acquired by going through the OAuth flow, but you can also get one for your own account by clicking the "Generate" button on the page for your Dropbox app. See https://www.dropbox.com/developers/blog/94/generate-an-access-token-for-your-own-account.)
Once you have an access token, your curl command should probably work, though I prefer --header "Authorization:Bearer abc123xyz" over putting the access token in a query parameter. Also, drop the Content-Type: multipart/mixed, since that's not what you're sending.
I'd also recommend "auto" instead of "dropbox," just because it always does the right thing regardless of the app type.
Here is working codes to upload file in dropbox via CURL request.
curl -X POST https://content.dropboxapi.com/2/files/upload \
--header "Authorization: Bearer <your token>" \
--header "Dropbox-API-Arg: {\"path\": \"/file_path.txt\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}" \
--header "Content-Type: application/octet-stream" \
--data-binary "#file_path.txt"

Cloudflare API PHP error

I am trying to add a record to Cloudflare via their API using PHP. For some reason, when I use the same code, the result sometimes gives me success, but at other times, it gives an error saying:
Could not route to /zones/dns_records, perhaps your object identifier is invalid?
and it gives the HTTP response code of 400 and 429. After searching those codes, I learned that the code 429 was something about too many requests. How could I solve this problem?
Unfortunately you are querying the wrong endpoint, the dns_records endpoint is in the format of /zones/:zone_identifier/dns_records
You will need to substitute :zone_identifier with the ID of a given zone, you can get this using the /zones endpoint.
So if you want to add a DNS record using cURL:
curl -X POST "https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a8372d0c353/dns_records" \
-H "X-Auth-Email: user#example.com" \
-H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" \
-H "Content-Type: application/json" \
--data '{"type":"A","name":"example.com","content":"127.0.0.1","ttl":120,"proxied":false}'

How to interpret CURL headers sent by user

I am creating an API using Laravel and Apache as the backend. My predicament is this, when I type a curl command in my terminal like,
curl -H "API KEY: NIKAPIKEY" --data "param1=value1&param2=value2" http://localhost:8888/awesome-calendar/public/config
How do I read the header API KEY in my php backend? Like I can read the POST parameters as $_REQUEST.
Answered by #nogad
The function apache_request_headers acts like the var_dump for all HTTP headers
http://php.net/manual/en/function.apache-request-headers.php

Spotify / cUrl command

I would like to add tracks to my spotify playlist by link. I´m not into cURL & php. Can some one help me on this? This is the curl code for adding a track to a list. I want to do it via php. Sorry, i don´t know where to start on this.
curl -X POST "https://api.spotify.com/v1/users/example/playlists/4435342DlTjJkiS2sZpd/tracks?position=0&uris=spotify%3Atrack%3A4iV5W9uYEdYUVa79Axb7Rh,spotify%3Atrack%3A1301WleyT98MSxVHPZCA6M"
-H "Accept: application/json"
-H "Authorization: Bearer exampleAccessToken"

Laravel 5 - Post JSON data using curl

I'm trying to POST data using command line using curl.
curl -v POST -d ' { "data1": "sample1", "data2": "sample2" } ' -H "Content-Type: application/json" -H "Authorization: BASIC vkslnkg561mZEqCq3l3RglAOAZ7d8XBeg2VjIAyC" http://localhost/Bowling/public/api/foo
When I try to POST from POSTMAN, I get the data back. But when I use curl, I'm getting a 500 Internal Server Error.
My routes.php
Route::any('api/foo', function () {
return json_encode("Some Data");
});
May I know is it something wrong with my curl statement or some authentication problem that I need to fix in Laravel.
I even tried with
curl -X POST -H "Content-Type: application/json" -d '{"key":"val"}' -H "Authorization: BASIC 0xD8BC2CLksEMh1ScRdG8dWanCDazYiQsGL7sYnl" http://localhost/Bowling/public/api/foo
There is nothing wrong in what you are doing. Its just the delay in your calls which is causing the token mismatch. I faced the same issue while manually testing my APIs via postman. Sad that Laravel doesn't have any decorator to exempt CSRF like Django.
On Postman, I was able to test my APIs issues by writing a route that returns a csrf token. I then copy that token and pass it as form-data in my next POST call. I couldn't find any other alternative.
Let me know if it works for you.

Categories