Curl-convert to a simple http request? - http

I have some curl request, in which i would like to build from it a basic POST request-authenticated (with headers etc), i couldn't find any tool that convert that :
curl -X POST 'https://api.twilio.com/2010-04-01/Accounts/xxxxxxxxxxxxx/Calls.json' \
--data-urlencode 'To=xxxxxxx65542' \
--data-urlencode 'From=+xxxxxxx4215' \
-d 'Url=https://api.twilio.com/2010-04-01' \
-d 'Method=GET' \
-d 'FallbackMethod=GET' \
-d 'StatusCallbackMethod=GET' \
-d 'Record=false' \
-u ACbe68cddxxxxxxxxxxxx3aba243cc4cdb:0f442xxxxxxxxxxxxxxxxxxx
So how would my POST request should look like ?

Okay so i was trying to figure out how to send a text message from an ESP8266 nodeMcu v0.9 module. It is capable of working like an arduino with the arduino ide 1.6.4.
Anyways, I found http://textbelt.com and it only shows a simple CURL way of sending the text message.
This is the CURL message it wants you to send
$ curl -X POST http://textbelt.com/text \ -d number=5551234567 \ -d "message=I sent this message for free with textbelt.com"
So to convert to a normal HTTP POST command i did the following. (this works in Arduino IDE)
number and message are String objects.
String messageToSend = "number="+number+"&message="+message;
client.print("POST /text HTTP/1.1\r\n");
client.print("Host: textbelt.com\r\n");
client.print("Content-Type: application/x-www-form-urlencoded\r\n");
client.print("Content-Length: ");
client.print(messageToSend.length());
client.print("\r\n\r\n");
client.print(messageToSend);
At first i tried without the Content-Type but that didn't seem to work. So i had to add the type of content i was sending as well.
If you were to monitor the network traffic with say WireShark you would see
POST /text HTTP/1.1\r\n
Host: textbelt.com\r\n
Content-Type: application/x-www-form-urlencoded\r\n
Content-Length: 48\r\n
\r\n
number=5551234567&message=this is a text message
I may have been able to use less text with text/plain as the content-type but i think it may need the application urlencoded type to work.
Hope this helps someone else trying to convert curl to http.

From the cURL man page :
-H, --header (HTTP) Extra header to use when getting a web page. You may specify any number of extra headers. Note that if you
should add a custom header that has the same name as one of the
internal ones curl would use, your externally set header will be
used instead of the internal one. This allows you to make even
trickier stuff than curl would nor‐ mally do. You should not replace
internally set headers without knowing perfectly well what you're
doing. Remove an internal header by giving a replacement without
content on the right side of the colon, as in: -H "Host:". If you send
the custom header with no-value then its header must be terminated
with a semicolon, such as -H "X-Custom- Header;" to send
"X-Custom-Header:".
curl will make sure that each header you add/replace is sent with the
proper end-of-line marker, you should thus not add that as a part of
the header content: do not add newlines or carriage returns, they will
only mess things up for you.
See also the -A, --user-agent and -e, --referer options.
This option can be used multiple times to add/replace/remove multiple
headers.
Amazon AWS makes heavy use of headers for authentication. A quick Google should lead to many examples such as this one from http://tmont.com/blargh/2014/1/uploading-to-s3-in-bash :
curl -X PUT -T "${file}" \
-H "Host: ${bucket}.s3.amazonaws.com" \
-H "Date: ${dateValue}" \
-H "Content-Type: ${contentType}" \
-H "Authorization: AWS ${s3Key}:${signature}" \
https://${bucket}.s3.amazonaws.com/${file}
All you need to do is adapt the AWS examples for your twilio use.
To make the example into a POST, just change PUT to POST and add your POST fields -d "field1=val1&field2=val2&field3=val3"

You can specify the headers using --header and the type of request (POST) by using -X / --request parameters.
Example:
curl --request POST --header "X-MyHeader: MyAuthenticatedHeader" www.stackoverflow.com
In your case it should be:
curl -X POST 'https://api.twilio.com/2010-04-01/Accounts/xxxxxxxxxxxxx/Calls.json' \
--header 'X-MyHeader: MyAuthenticatedHeader' \
--data-urlencode 'To=xxxxxxx65542' \
--data-urlencode 'From=+xxxxxxx4215' \
-d 'Url=https://api.twilio.com/2010-04-01' \
-d 'Method=GET' \
-d 'FallbackMethod=GET' \
-d 'StatusCallbackMethod=GET' \
-d 'Record=false' \
-u ACbe68cddxxxxxxxxxxxx3aba243cc4cdb:0f442xxxxxxxxxxxxxxxxxxx

Related

How to use multiple double quotes in a udev rule

I am trying to send a post request once a usb device is connected to the Pi using udev rules.
My first idea was that the udev rules should look like this:
SUBSYSTEM=="tty", ATTRS{serial}=="GT633DNJ", RUN+="curl --header "Content-Type: application/json" --request POST --data '{"name":"boardname"}' http://0.0.0.0:8080/post"
An error was produced in the syslog once I connected the usb device to the py and documented in the syslog
Feb 16 16:02:24 localhost systemd-udevd[204]: invalid key/value pair in file /etc/udev/rules.d/60-board.rules on line 3, starting at character 193 ('C')
which I guess means that it has considered the RUN part to look like this
RUN+="curl --header " and it didn't know what to do with the rest of the line
Content-Type: application/json" --request POST --data '{"name":"boardname"}' http://0.0.0.0:8080/post"
I tried also to escape the double quotes with a \ like this:
SUBSYSTEM=="tty", ATTRS{serial}=="GT633DNJ", RUN+="curl --header \"Content-Type: application/json\" --request POST --data '{\"name\":\"saf85xx_03\", \"helper_host\":\"'hostname'\"}' http://0.0.0.0:8080/post"
But it didn't work neither
Is it possible to send the post request from inside the RUN without having to put the curl request in a bash script and call the script from inside RUN?

How to get the http request to work on my browser

I am using the here-api to get a 7 day forecast for my application. When I copy and paste the code onto my browser from the Here documentation using my app-code and app-id, it says that my app-id and app-code are invalid. I am wondering why this is happening since i have an account with Here and am using the code they provided.
I have already tried switching them.
https://weather.api.here.com/weather/1.0/report.json
?app_id={xxxxxx}
&app_code={xxxxxx}
&product=forecast_7days_simple
&latitude=41.83
&longitude=-87.68
When i enter this request i expected json in return but instead get a page that says my code is invalid. I am using safari but have tried chrome and i still get the same problem
Make sure to remove the curly brackets around the credentials, and to remove newlines (if any) that were added in the documentation for readability.
If you do have valid credentials, the following request should work:
https://weather.api.here.com/weather/1.0/report.json?app_id=xxxx&app_code=yyyy&product=forecast_7days_simple&latitude=41.83&longitude=-87.68
Use the query like below. For any query you can use the swagger here . And, from the "view code" button you can download either the curl or the jQuery snippet.
curl \
-X GET \
-H 'Content-Type: *' \
--get 'https://weather.api.here.com/weather/1.0/report.json' \
--data-urlencode 'product=forecast_7days_simple' \
--data-urlencode 'latitude=52.516' \
--data-urlencode 'longitude=13.389' \
--data-urlencode 'oneobservation=true' \
--data-urlencode 'app_id={YOUR_APP_ID}' \
--data-urlencode 'app_code={YOUR_APP_CODE}'

What does -d stand for in curl request?

I am trying to send this HTTP request in Postman application:
curl -v https://api.someurl.com/z1/lists \
-X POST \
-H "Content-Type: application/json" \
-H "Authorization: authorization" \
-d '{ "list_id": "DXVBDAD" }'
Any body knows what -d stands for? and where should I put it in Postman?
The documentation says this:
(HTTP) Sends the specified data in a POST request to the HTTP server[...]
So this will be the body of your POST request. In Postman you have to put it into the 'body' field. There select 'raw' and then select 'application/json'.
Because that's the Content-Type of your request, specified with -H.
The -d or --data option makes the curl command send data in POST request to the server. This option makes the curl command pass data to the server using content-type (JSON in your case) just as the browser does when a user submits a form.

Pass parameters to Airflow Experimental REST api when creating dag run

Looks like Airflow has an experimental REST api that allow users to create dag runs with https POST request. This is awesome.
Is there a way to pass parameters via HTTP to the create dag run? Judging from the official docs, found here, it would seem the answer is "no" but I'm hoping I'm wrong.
I had the same issue. "conf" value must be in string
curl -X POST \
http://localhost:8080/api/experimental/dags/<DAG_ID>/dag_runs \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/json' \
-d '{"conf":"{\"key\":\"value\"}"}'
Judging from the source code, it would appear as though parameters can be passed into the dag run.
If the body of the http request contains json, and that json contains a top level key conf the value of the conf key will be passed as configuration to trigger_dag. More on how this works can be found here.
Note the value of the conf key must be a string, e.g.
curl -X POST \
http://localhost:8080/api/experimental/dags/<DAG_ID>/dag_runs \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/json' \
-d '{"conf":"{\"key\":\"value\"}"}'
This is no longer true with the stable REST API.
You can do something like -
curl --location --request POST 'localhost:8080/api/v1/dags/unpublished/dagRuns' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic YWRtaW46YWRtaW4=' \
--data-raw '{
"dag_run_id": "dag_run_1",
"conf": {
"key": "value"
}
}'
I understand that the question is asked for experimental API but this question is the top search result for airflow REST API.

How can I use the Pingdom API to pause and resume checks from bash?

I'm writing a quick and dirty deployment script and would like to disable and reenable a Pingdom check as part of it. How do I do that using something like cURL?
To pause a check:
curl -X PUT -u 'your#email:yourpassword' -H 'Content-Type: application/json' -H 'App-Key: yourapplicationkey' -d 'paused=true' https://api.pingdom.com/api/2.0/checks/checkid
To resume a check:
curl -X PUT -u 'your#email:yourpassword' -H 'Content-Type: application/json' -H 'App-Key: yourapplicationkey' -d 'paused=false' https://api.pingdom.com/api/2.0/checks/checkid
Replace your#email with your pingdom email.
Replace yourpassword with your pingdom password.
Replace yourapplicationkey with a generated key from the "Sharing" section in your account.
Replace checkid with the numeric ID you see in the browser URL when you click on your check in the Pingdom UI.
You can also use modern way - just API key instead of using also email/password.
First, generate your own API key in https://my.pingdom.com/app/api-tokens and then you can use curl commands like for pausing:
curl -X PUT \
https://api.pingdom.com/api/3.1/checks \
-H 'Authorization:Bearer YOURAPIKEY' \
-d 'paused=true&checkids=777'
or for resuming:
curl -X PUT \
https://api.pingdom.com/api/3.1/checks \
-H 'Authorization:Bearer YOURAPIKEY' \
-d 'paused=false&checkids=777'
Replace YOURAPIKEY with your real API key and 777 with valid check ID.
checkids can be also omitted, then all checks will be modified.

Resources