How do I write this cURL request correctly? - php

I am trying to complete a Facebook messenger bot based on this tutorial here: https://github.com/voronianski/simon-le-bottle/blob/master/GUIDE.md
As you can see in the last instruction, I must send a page access token via this cURL request in the following format:
curl -i \
-H "Content-Type: application/json" \
-X POST \
-d "{\"verifyToken\": \"YOUR VERIFY TOKEN\", \"token\": \"YOUR PAGE ACCESS TOKEN\"}" \
https://YOUR_GENERATED_URL.now.sh/token
Of course I replaced "YOUR VERIFY TOKEN" with the token I've generated and "YOUR PAGE ACCESS TOKEN" with the page access token that I've generated and "YOUR GENERATED URL" with my own url. However, I have tried multiple times and gotten various errors.
The first time I just tried copying and pasting the tokens and url into the input space and pasting the cURL request in that format. I received the following errors:
curl: (6) Could not resolve host:
-bash: -H: command not found
-bash: -X: command not found
Basically all I received were commands not found. Then, I tried a different approach and removed the new line tabs and leaving only spaces like so
curl -i \ -H "Content-Type: application/json" \ -X POST \ -d "{\"verifyToken\": \"randomverifytokenhere\", \"token\": otherrandomtokenhere\"}" https://myspecificurl.now.sh/token
Where of course I had actual working tokens and a website yet again. The commands seemed to work, but I got a whole new crop of errors like so:
curl: (6) Could not resolve host: -H
curl: (6) Could not resolve host: Content-Type
curl: (6) Could not resolve host:
curl: (6) Could not resolve host: -d
curl: (6) Could not resolve host: "verifyToken"
curl: (6) Could not resolve host: "token"
HTTP/1.1 403 Forbidden
Server: nginx
Date: Wed, 01 Jun 2016 21:51:10 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 9
Connection: keep-alive
X-Powered-By: Express
If anyone could tell me exactly how I need to format and paste it into terminal to point where it works, that would be very helpful. Or if there's an easier, better way, I would definitely appreciate that. Every other step, up until the very last instruction, has been completed. The website has already been verified with webhook by Facebook, and I only use valid tokens and the specific website generated with "NOW". Thank you very much everyone, and I appreciate the help.

Make sure you have no white space after the backslash at the end of the lines. When I highlight your command, there appears to be a space. If you remove those your problem should go away.
If you run it as a single line command, remove the backslashes that are at the end of each line.

Related

Microsoft-Cognitive Text Translation API

AZURE - The official documentation and examples for PHP is not working, help?
I am using the code at this link:
https://github.com/courtney7/HTTP-Code-Samples/blob/37a4431f75397e1ccc6ee3f62ef14b3909a2dc85/PHP/PHPAzureToken.php
Always test and debug APIs using curl (i.e. take a curl first approach). Here's an example that works on my Windows 10 system. Here's the curl that gets a token.
curl -k --data "" "https://api.cognitive.microsoft.com/sts/v1.0/issueToken" -H "Ocp-Apim-Subscription-Key:<your Ocp-Apim-Subscription-Key>"
Example to get token and call text translate.
curl -k --data "" "https://api.cognitive.microsoft.com/sts/v1.0/issueToken" -H "Ocp-Apim-Subscription-Key:<your Ocp-Apim-Subscription-Key>" > Bearer.txt
set /p Bearer= < Bearer.txt
curl -H "Authorization: Bearer %Bearer%" "https://api.microsofttranslator.com/v2/Http.svc/Translate?Text=Hello+world.&From=en&To=es"
Make sure you are calling the correct endpoint "https://api.cognitive.microsoft.com/sts/v1.0/issueToken"

Shell variable passed to -d option of curl not working correctly

I am trying to use PATCH method in CURL command use -d option. my code is like below:
IFS=" ";
while read line
do
array=($line);
json_string="{ \"attributes\": { \"member_id\": ${array[0]}, \"is_student\": ${array[1]} }}"
curl -v --fail --silent --show-error --request PATCH -H "Content-Type: application/json" --user $API_KEY:$API_SECRET -d $json_string $PROFILE_URL/${array[0]} --trace-ascii /dev/stdout
done < $file
the input file is txt file which has two columns:
123 0
I am using space as a line separator.
The problem is when I use $json_string I get this error:
== Info: Rebuilt URL to: "attributes":/
== Info: Could not resolve host: "attributes"
== Info: Closing connection 0
curl: (6) Could not resolve host: "attributes"
curl: (3) [globbing] unmatched brace in column 1
== Info: Rebuilt URL to: "member_id":/
== Info: Could not resolve host: "membe
r_id"
== Info: Closing connection 1
curl: (6) Could not resolve host: "member_id"
== Info: Rebuilt URL to: 123,/
== Info: Could not resolve host: 123,
and
=> Send data, 2 bytes (0x2)
0000: "{
== Info: upload completely sent off: 2 out of 2 bytes
but when I use "$json_string" ($json_string surrounded with double quote) it works fine. any ideas?
Enclose your variable json_string in double quotes to prevent word splitting by shell:
curl -v --fail --silent --show-error --request PATCH -H "Content-Type:application/json" --user "$API_KEY:$API_SECRET" -d "$json_string" "$PROFILE_URL/${array[0]}" --trace-ascii /dev/stdout
You can read the line directly into your array this way:
#!/bin/bash
while read -a line; do
# your logic
done < "$file"
Read more about word spitting here: Bash Best Practices - Quoting

Command works in cmd prompt but not in Powershell, what am I doing wrong?

So I've been tearing my hair out all day regarding this one. I have a curl command which I have working on a windows machine which pulls logs from Cloudflare, this works.
curl -sv -o logname.log.gz -X GET -H "Accept-encoding: gzip" -H "X-Auth-Email: myemail#email.com" -H "X-Auth-Key: 12345" "https://api.cloudflare.com/client/v4/zones/987654/logs/requests?start=1481509909&end=1481538709"
I'm trying to import this into powershell with the end goal of making the start and end time parameters different based on the current time, however I simply cannot get the command to run in powershell, I've tried various different tracks with this being the latest and most simple.
cmd.exe /c 'curl -sv -o logname.log.gz -X GET -H "Accept-encoding: gzip" -H "X-Auth-Email: myemail#email.com" -H "X-Auth-Key: 12345" "https://api.cloudflare.com/client/v4/zones/987654/logs/requests?start=1481509909&end=1481538709"'
Which gives me this error
{ [11971 bytes data]
* Failed writing body (0 != 11963)
* Failed writing data
* Curl_http_done: called premature == 1
* Closing connection 0
* schannel: shutting down SSL/TLS connection with api.cloudflare.com port 443
* schannel: clear security context handle
I already know that my unix timestamp is a bit off and plan on fixing that next but what I cannot understand is why the same command works through the command prompt and not through Powershell.
Would someone be able to help?
Thank you
Dealing with parameters to native commands in PowerShell can be a minefield because you have to deal with quoting and special characters for both, sometimes nested.
It's probably safer to use Start-Process in PowerShell and give it an array of parameters:
Start-Process curl.exe -ArgumentList '-sv','-o','logname.log.gz','-X','GET','-H','Accept-encoding: gzip','-H','X-Auth-Email:','myemail#email.com','-H','X-Auth-Key:','12345','https://api.cloudflare.com/client/v4/zones/987654/logs/requests?start=1481509909&end=1481538709'
But what you should realy do, is check out Invoke-WebRequest which will ultimately be much easier.
$body = #{
start = 1481509909
end = 1481538709
}
$headers = #{
'Accept-Encoding' = 'gzip'
'X-Auth-Email' = 'myemail#email.com'
'X-Auth-Key' = '12345'
}
$response = Invoke-WebRequest -Uri 'https://api.cloudflare.com/client/v4/zones/987654/logs/requests' -OutFile logname.log.gz -Body $body -Headers $headers
Note this is not tested at all, but should be a good starting point.

Check 301 http response with curl

I'm migrating a web site. And I need to do the migration of more than 600 old url to a new type of url
with symfony routing and 301 permanent redirection.
example of one of my url:
redirect_test-url:
path: /test-url
defaults:
_controller: FrameworkBundle:Redirect:urlRedirect
path: /category/test-url
permanent: true
The redirect seems to work. But I need to be sure that all my old url are redirected with a 301 redirect.
I think I can do a script with php and curl to check all the url. But I don't know how can get the curl result.
Here is my curl request:
curl https://local.dev -k
But my terminal jut send me back html.
How can I get the http status code response to store it in a database?
You can dump the headers of the response with the -D option:
curl -s -D - http://www.martar.fr -o /dev/null
Response:
HTTP/1.1 301 Moved Permanently
Date: Fri, 07 Oct 2016 07:50:51 GMT
Server: Apache/2.4.10 (Debian)
Location: https://www.martar.fr/
Content-Length: 309
Content-Type: text/html; charset=iso-8859-1
-s is for Silent mode (don't output anything)
I think perhaps you want something similar to this:
curl -sI https://example.com -w '%{response_code}:%{redirect_url}\n' -o /dev/null
As a bonus, this gets you both the redirect code and the redirect target (if any).
curl writeout variables in general extremly useful
curl -sI https://duravit.de -w '%{response_code}:%{redirect_url}:%header{server}\n' -o /dev/null
https://everything.curl.dev/usingcurl/verbose/writeout#variables

curl: (26) couldn't open file

I am getting this error, when I am trying to call a box api through curl.
curl: (26) couldn't open file
Can't find why! I am calling this api with a correct file name-
curl https://upload.view-api.box.com/1/documents \
-H "Authorization: Token YOUR_API_TOKEN" \
-H "Content-type: multipart/form-data" \
-F file=#A_correct_file_name
I have seen all the three already asked questions but 2 of them are unanswered and one is specific to facebook.
cURL error 26 couldn't open file
Fatal error: Uncaught CurlException: 26: couldn't open file "" thrown in
Getting Fatal Error Uncaught CurlException: 26: couldn't open file
I was having a similar problem after changing to PHP 5 (I was using the # upload method and it was deprecated, so I had to start using CURLFile), and the solution to my problem was found in this stack.
Solution: curl upload won't work with relative paths, use the full path instead
Sorry guys! My bad. I had not included extension of the file in the file name. After including it, it worked. I am answering this in case someone does the same mistake in future.
For my case, using relative path didn't work. But changing it to a absolute path fixes it.
This failed
curl -i -X POST -H "Content-Type: multipart/form-data" \
-F "file=#~/Downloads/xxx.csv" http://localhost:6708/upload
This worked
curl -i -X POST -H "Content-Type: multipart/form-data" \
-F "file=#/Users/myself/Downloads/xxx.csv" http://localhost:6708/upload
I was having this problem this morning but I solved with this
fileUpload=#\"file, with comma .txt\"
So you have to put some double quotes around the file name if you have commas in the file name ( \" )
I had a similar problem with relative paths like #Guilherme did too. I was running my bash script with:
bash test/script.sh
However, my file was in the same directory level as the bash script and not the test directory (where I was calling bash from), so the script could not find my file.
Solution for me:
1. cd into /test and run bash script.sh from there
2. Use absolute paths
I am using PlateRecognizer.com to secure license plate information from pics that I have isolated from videos from my dashcam. I was having so many problems with the exact code. PlateRecognizer did not provide the correct information in their help section.
I work using my Windows 10 laptop. This code is what finally worked for me:
curl -F upload=#/”Users/Me/Desktop/Driver1.mp4” -F regions=us-az -H “Authorization: Token 4892e779f97d879df6453” https://api.platerecognizer.com/v1/plate-reader/
Make note of the double quote marks around the file path and name - not single quote marks.
Luis Cruz is correct: quotes are required around the filename, but I needed to use the 'file' keyword, not 'fileUpload', as is it shown in the question.
curl -X GET \
-H 'Content-Type: multipart/form-data' \
-F file=#"/path/to/file.ext" \
'http://host:port/path/to/dir'

Categories