Curl post with multipart/form-data and JSON - linux

Is there a way to use curl to do a multipart/form-data post and specify that the data is application/json? It doesn't have to be curl.

This is what I use, and it works fine
curl -v -H "Content-Type:multipart/form-data"
-F "someName={\"your\":\"json\"};type=application/json"
Note: I'm on Windows, hence the requirement for double quotes
Or
-F "someName=#someJsonFile.json;type=application/json"

Related

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.

URL encoding with SharePoint URL and cURL

I am new to using CURL. I am trying to connect to a SharePoint URL and pull the data in the form of json. My URL looks like this:
.../_api/web/lists/GetByTitle('titles list')/items
If I give the URL as it is without any encoding, it fails with a HTTP/1.1 400 Bad Request error.
I tried using -G and --data-urlencode like below:
curl -v -G -L --ntlm --user user:password -H 'Accept: application/json;odata=verbose' ".../_api/web/lists/GetByTitle" --data-urlencode "('titles list')" -d "/items"
doing this will convert my URL to .../_api/web/lists/GetByTitle?%28%27titles%20list%27%29&/items
But it fails with HTTP/1.1 404 Not Found since using -G will append to the URL with a ? and & . Putting ? and & to append to the URL will give me a different URL and hence the 404 not found error.
I have no issues accessing other end points like ../_api/web/lists since there is no need to encode it I guess.
How do I properly encode my URL and get the data without any errors?
Any help would be appreciated. Thank you.
I was able to solve the issue by directly giving URL with the encoded characters to cURL command. Like this:
curl -v -L --ntlm --user user:password -H 'Accept: application/json;odata=verbose' ".../_api/web/lists/GetByTitle%28%27titles%20List%27%29/items"
I hope this helps someone. I know I am no expert with linux or cURL and any better answers to this are welcome.

curl command in linux

How we can add JSON parameters in a curl command to fetch data ?
PS: I have tried to fetch data from some source using curl command but it requires passing JSON parameters in it. So how I will accomplish it ?
I think mentioning content type as json and passing data will do the things right
curl -H "Content-Type: application/json" -d '{"key1":"value1","key2":"value2"}' http://domain.com/rest/path/here
Above will work for GET Method, for POST method
curl -H "Content-Type: application/json" -X POST -d '{"key1":"value1","key2":"value2"}' http://domain.com/rest/path/here

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"

Difference between curl calls

What is the difference between this two curl calls:
curl -X POST -d "/path/file.txt" http://api-path
curl -X POST -d "#/path/file.txt" http://api-path
The first form sends the string "/path/file.txt" as data, probably not what you want. The # makes curl interpret it as a filename to read from. See http://curl.haxx.se/docs/manpage.html, search for the "--data" option.

Resources