How to post raw body data with curl? - linux

Before you post this as a duplicate; I've tried many of the suggestions I found around SO.
So far I've been using postman to post data to a Java web service. That works great as follows:
I now want to do the same using curl, so I tried it using the following ways:
$ curl -X POST --data "this is raw data" http://78.41.xx.xx:7778/
$ curl -X POST --data-binary "this is raw data" http://78.41.xx.xx:7778/
$ curl -X POST --data "#/home/kramer65/afile.txt" http://78.41.xx.xx:7778/
$ curl -X POST --data-binary "#/home/kramer65/afile.txt" http://78.41.xx.xx:7778/
Unfortunately, all of those show an empty raw body on the receiving side.
Does anybody know what I'm doing wrong here? How is my curl request different from my postman request? All tips are welcome!

curl's --data will by default send Content-Type: application/x-www-form-urlencoded in the request header. However, when using Postman's raw body mode, Postman sends Content-Type: text/plain in the request header.
So to achieve the same thing as Postman, specify -H "Content-Type: text/plain" for curl:
curl -X POST -H "Content-Type: text/plain" --data "this is raw data" http://78.41.xx.xx:7778/
Note that if you want to watch the full request sent by Postman, you can enable debugging for packed app. Check this link for all instructions. Then you can inspect the app (right-click in Postman) and view all requests sent from Postman in the network tab :

Related

How can i send a base64 image to Azure Face-API with curl?

as my question mention it, how do i need to change this statement to send an image in base64 to Azure?
curl -H "Ocp-Apim-Subscription-Key: ***hidden***" "https://***hidden***.cognitiveservices.azure.com/face/v1.0/detect?detectionModel=detection_03&returnFaceId=true&returnFaceLandmarks=false"
-H "Content-Type: application/json" --data-ascii "{\"url\":\"https://upload.wikimedia.org/wikipedia/commons/c/c3/RH_Louise_Lillian_Gish.jpg\"}"
Thank you!
how do i need to change this statement to send an image in base64 to Azure?
As per this documentation, your curl command seems fine.
Alternatively, you can try as suggested by muru:
'{"image" : "'"$( base64 ~/Pictures/1.jpg)"'"}
Updated answer:
As per Use the Face client library:
curl -v -X POST "https://westus.api.cognitive.microsoft.com/face/v1.0/detect?returnFaceId=true&returnFaceLandmarks=false&returnFaceAttributes={string}&recognitionModel=recognition_04&returnRecognitionModel=false&detectionModel=detection_03&faceIdTimeToLive=86400" -H "Content-Type: application/json" -H "Ocp-Apim-Subscription-Key: {subscription key}" --data-ascii "{\"url\":\"https://csdx.blob.core.windows.net/resources/Face/Images/identification1.jpg\"}"
You can refer to similar issues: can't send/request base64 to azure face api with python and Face API need support for base64 string or Local File URL or Native File URL instead of Image URL
curl -X POST "https://.cognitiveservices.azure.com/face/v1.0/detect" -H "Content-Type: application/octet-stream" -H "Ocp-Apim-Subscription-Key: 27e993*********6aaa2464" --data-binary #'/home/rafael/Downloads/person.jpg'
is the solution that works. As far as I can tell, the documentation seems outdated.

Unable to Log Python List Objects as Message in Graylog

I am trying to send Python List as Log Message to Graylog. The approach that i am using is "Sending GELF messages via HTTP using curl" mentioned in http://docs.graylog.org/en/2.4/pages/gelf.html
But when i send data as below:
curl -X POST -H 'Content-Type: application/json' -d '{"log_type":"debug", "short_message": "[1,4,5,2]", "block_id":"TEST_LOGGING"}' 'http://<host>:12201/gelf'
it works perfectly fine.
Where as on sending message as below Logs nothing.
curl -X POST -H 'Content-Type: application/json' -d '{"log_type":"debug", "short_message": [1,4,5,2], "block_id":"TEST_LOGGING"}' 'http://<host>:12201/gelf'
I am unable to figure out the issue.
Try using 'graypy'. It lets you post logs to graylog using TCP port 12201 for the input streams creating. It also lets you add additional parameters, that you can tag to a message using adapters. The documentation has examples for adapters.

POST csv/Text file using cURL

How can I send POST request with a csv or a text file to the server running on a localhost using cURL.
I have tried curl -X POST -d #file.csv http://localhost:5000/upload but I get
{
"message": "The browser (or proxy) sent a request that this server could not understand."
}
My server is flask_restful API. Thanks a lot in advance.
There are many alternate ways to accomplish this. One way is
I have used the following:
curl -F ‘data=#<file_location>’ <URL>
Eg. curl -F data=#data.csv localhost:5000/h
Your command can also be changed slightly like this
curl -X POST -H 'Content-Type: text/csv' -d #file.csv http://localhost:5000/upload
The above is one of the many ways.It can be sent either as a part of form or data, or multipart, etc. You can refer Medium Post
Curl's default Content-Type is application/x-www-form-urlencoded so your problem is probably that the data you are POSTing is not actually form data. It might work if you set the content type header properly:
-H "Content-Type: text/csv"
Though it does depend on the server.

GET request with curl. No errors, but Content-Length equals 0

Simple GET request with curl returns empty body (Content-Length: 0):
curl -v https://www.flyorientthai.com/booking/en/index.php
On the other hand wget can handle that url just fine:
wget https://www.flyorientthai.com/booking/en/index.php
What's wrong with curl?
Turned out 'Connection: Keep-Alive' header is required. It's added by default to request with wget but not with curl.
For anyone using "Copy as cURL" in the Chrome developer tools network tab to generate a curl command — one of the lines in the generated curl command is a header that looks something like...
-H 'If-None-Match: W/"18f6a6-6p8AL7X/p71IhN/WztZm60Aue4k"'
That causes the server to return an empty 304 response if nothing's changed. Just whip it out.
Let's try "Content-Type: application/x-www-form-urlencodeds"

How to POST URL in data of a curl request

I am trying to post two parameters using curl, path and fileName:
curl --request POST 'http://localhost/Service' --data "path='/xyz/pqr/test/'&fileName='1.doc'"
I know something is wrong in this.
I have to use something like URLEncode. I tried many things still no luck.
Please give an example how can I post the url in data of curl request.
Perhaps you don't have to include the single quotes:
curl --request POST 'http://localhost/Service' --data "path=/xyz/pqr/test/&fileName=1.doc"
Update: Reading curl's manual, you could actually separate both fields with two --data:
curl --request POST 'http://localhost/Service' --data "path=/xyz/pqr/test/" --data "fileName=1.doc"
You could also try --data-binary:
curl --request POST 'http://localhost/Service' --data-binary "path=/xyz/pqr/test/" --data-binary "fileName=1.doc"
And --data-urlencode:
curl --request POST 'http://localhost/Service' --data-urlencode "path=/xyz/pqr/test/" --data-urlencode "fileName=1.doc"
I don't think it's necessary to use semi-quotes around the variables, try:
curl -XPOST 'http://localhost/Service' -d "path=%2fxyz%2fpqr%2ftest%2f&fileName=1.doc"
%2f is the escape code for a /.
http://www.december.com/html/spec/esccodes.html
Also, do you need to specify a port? ( just checking :) )

Resources