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?
Related
I can do single line GET requests with sim800 but I couldn't understand to use this API.
curl --request GET \
--url 'https://api.collectapi.com/pray/all?data.city=istanbul' \
--header 'authorization: apikey 3tXXXMYAPIKEYq9:1RxvCMYAPIKEYgAG' \
--header 'content-type: application/json'
It shows me how to use it with curl but I can't understand how can I make this as single line on Sim800
AT+HTTPINIT<\r><\r><\n>OK<\r><\n>
AT+HTTPPARA="CID",1<\r><\r><\n>OK<\r><\n>
AT+HTTPPARA="URL","https://api.collectapi.com/pray/all?data.city=kahramanmaras"<\r><\r><\n>OK<\r><\n>
AT+HTTPPARA="USERDATA","authorization: apikey 3tvAtuSxxxxxx4zDlq9:1RxvC4fQTxxxxxGbgAG"<\r><\r><\n>OK<\r><\n>
AT+HTTPPARA="CONTENT","application/json"<\r><\r><\n>OK<\r><\n>
AT+HTTPACTION=0<\r><\r><\n>OK<\r><\n>
<\r><\n>
+HTTPACTION: 0,603,0<\r>
I'm doing something wrong but I don't understand :(
Try the at+httpread command to find out what is the error is it looks like a custom error rather than a HTTP error
I have an image in a URL like https://i.postimg.cc/GpDskmSG/sdssds.png and I want to send it over to be stored at nft.storage. Could I send it directly as a URL instead of a file from a path? I tried the below but it only stores the image URL.
curl -H "Authorization: Bearer eyJhbGciOiJIwetertyrtyUzI1NiIsInR5cCI6Ikp" -H "Content-Type: image/png" --data "https://i.postimg.cc/GpDskmSG/sdssds.png" --url "https://api.nft.storage/upload"
P/S: I'm testing on windows curl.
According to the API docs (https://nft.storage/api-docs/), it will accept multipart/form-data for this type of POST.
You can try using -F/--form instead, for which a good example exists on the curl man page (https://linux.die.net/man/1/curl):
curl -F "web=#index.html;type=text/html" url.com
There is a sample in Jira API doc:
curl -D- -X GET -H "Authorization: Basic ZnJlZDpmcmVk" -H "Content-Type: application/json" "http://kelpie9:8081/rest/api/2/issue/QA-31"
What exactly does the -D- parameter mean? There is no description in curl documentation.
I'm also not sure whether -D- and -D mean the same thing.
It dumps headers to stdout.
The -D flag dumps headers to a file, and the following - instructs it to dump to stdout. From the linked spec:
-D, --dump-header
Write the protocol headers to the specified file.
It doesn't specify for this option, but this works like other options that take a filename:
Use "-" as filename to have the output sent to stdout.
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
I need to make a POST request via cURL from the command line. Data for this request is located in a file. I know that via PUT this could be done with the --upload-file option.
curl host:port/post-file -H "Content-Type: text/xml" --data "contents_of_file"
You're looking for the --data-binary argument:
curl -i -X POST host:port/post-file \
-H "Content-Type: text/xml" \
--data-binary "#path/to/file"
In the example above, -i prints out all the headers so that you can see what's going on, and -X POST makes it explicit that this is a post. Both of these can be safely omitted without changing the behaviour on the wire. The path to the file needs to be preceded by an # symbol, so curl knows to read from a file.
I need to make a POST request via Curl from the command line. Data for this request is located in a file...
All you need to do is have the --data argument start with a #:
curl -H "Content-Type: text/xml" --data "#path_of_file" host:port/post-file-path
For example, if you have the data in a file called stuff.xml then you would do something like:
curl -H "Content-Type: text/xml" --data "#stuff.xml" host:port/post-file-path
The stuff.xml filename can be replaced with a relative or full path to the file: #../xml/stuff.xml, #/var/tmp/stuff.xml, ...
If you are using form data to upload file,in which a parameter name must be specified , you can use:
curl -X POST -i -F "parametername=#filename" -F "additional_parm=param2" host:port/xxx
Most of answers are perfect here, but when I landed here for my particular problem, I have to upload binary file (XLSX spread sheet) using POST method, I see one thing missing, i.e. usually its not just file you load, you may have more form data elements, like comment to file or tags to file etc as was my case. Hence, I would like to add it here as it was my use case, so that it could help others.
curl -POST -F comment=mycomment -F file_type=XLSX -F file_data=#/your/path/to/file.XLSX http://yourhost.example.com/api/example_url
I was having a similar issue in passing the file as a param. Using -F allowed the file to be passed as form data, but the content type of the file was application/octet-stream. My endpoint was expecting text/csv.
You are able to set the MIME type of the file with the following syntax:
-F 'file=#path/to/file;type=<MIME_TYPE>
So the full cURL command would look like this for a CSV file:
curl -X POST -F 'file=#path/to/file.csv;type=text/csv' https://test.com
There is good documentation on this and other options here: https://catonmat.net/cookbooks/curl/make-post-request#post-form-data
I had to use a HTTP connection, because on HTTPS there is default file size limit.
https://techcommunity.microsoft.com/t5/IIS-Support-Blog/Solution-for-Request-Entity-Too-Large-error/ba-p/501134
curl -i -X 'POST' -F 'file=#/home/testeincremental.xlsx' 'http://example.com/upload.aspx?user=example&password=example123&type=XLSX'