CURL Command Line URL Parameters - http

I am trying to send a DELETE request with a url parameter using CURL. I am doing:
curl -H application/x-www-form-urlencoded -X DELETE http://localhost:5000/locations` -d 'id=3'
However, the server is not seeing the parameter id = 3. I tried using some GUI application and when I pass the url as: http://localhost:5000/locations?id=3, it works. I really would rather use CURL rather than this GUI application. Can anyone please point out what I'm doing wrong?

The application/x-www-form-urlencoded Content-type header is not required (well, kinda depends). Unless the request handler expects parameters coming from the form body. Try it out:
curl -X DELETE "http://localhost:5000/locations?id=3"
or
curl -X GET "http://localhost:5000/locations?id=3"

#Felipsmartins is correct.
It is worth mentioning that it is because you cannot really use the -d/--data option if this is not a POST request. But this is still possible if you use the -G option.
Which means you can do this:
curl -X DELETE -G 'http://localhost:5000/locations' -d 'id=3'
Here it is a bit silly but when you are on the command line and you have a lot of parameters, it is a lot tidier.
I am saying this because cURL commands are usually quite long, so it is worth making it on more than one line escaping the line breaks.
curl -X DELETE -G \
'http://localhost:5000/locations' \
-d id=3 \
-d name=Mario \
-d surname=Bros
This is obviously a lot more comfortable if you use zsh. I mean when you need to re-edit the previous command because zsh lets you go line by line. (just saying)

Related

Can I have conflict in Riak with using a vclock getting by a get

I want to know if I can have conflict in this scenario :
#!/usr/bin/env bash
curl -XPUT -d '{"bar":"baz"}' \
-H "Content-Type: application/json" \
http://127.0.0.1:8098/riak/obj/1
response=$(curl -I http://127.0.0.1:8098/riak/obj/1 | grep 'X-Riak-Vclock:' | egrep -o ' .*$')
curl -v -XPUT -d '{"bar":"foo"}' \
-H "Content-Type: application/json" \
-H "X-Riak-Vclock: $response" \
http://127.0.0.1:8098/riak/obj/1
In some words :
First I have no object for the key 1 I put the {"bar":"baz"} value with the PUT of the http api.
Then, I read the value with a get. And I store the vclock in variable.
And finaly I put a new value {"bar":"foo"} for the key 1
Is there a case where I can have {"bar":"baz"} for the key 1 ? If Riak has a conflict, it will be resolve with vclock ?
Thanks !
It depends how your Riak database is configured, either globally or if you changed the default configuration of the bucket you're using. If you keep the default config, your second PUT (with the vclock) might:
- fail, if someone updated the key behind your back (rare), and the vclock data you have is already obsolete. You need to re-read the value and update it. Best is to have a retry mechanism.
- fail, if the write consistency constrains you have is too strict, and too many nodes are down (rare). Usually the default read and write config are sane.
- succeed, if the vclock data is still valid for this key (most of the time)
In case it succeeds, it might be that the network topology was in a split-brain situation. In this case, Riak will solve the issue itself using v-clock data.

How to convert Curl to url with headers

I have this command in cURL and it works
curl -X GET \
-H "X-Parse-Application-Id: APP_ID" \
-H "X-Parse-REST-API-Key: API_KEY" \
-G \ https://parseapi.back4app.com/classes/Books
I want create a url that will execute the same way on the browser.
The website that I'm working with is back4app.
There is no way to achieve the same thing with just a URL. This relies on HTTP Headers (both -H parameters) that can't be translated to something else easily. To set these headers in a web browser, you'd at least need to execute JavaScript.
There might be a way if the target API supports reading the same fields from the url in some way (technically, there's no reason not to do this). I haven't found something on that topic in their docs, though.

How to submit a GET request with json input using apache bench?

The request for our service looks something like this:
GET http://[SERVICE]/Node:[id].Build?format=mime1,mime2,...,mimeN&template-id=[templateid]
Accept: multipart/mixed
Content-Type: application/json
body: json document
I am attempting to use ApacheBench to test benchmark this. Here is the call I am using:
ab -n 10 -c 2 -T 'application/json' -H 'Accept: multipart/mixed' 'http://phx5qa01c-02b0.stratus.phx.qa.ebay.com/.Build?format=text/html,text/plain&template-id=29b1468f-c8c3-db23-2f6f-74e112795540'
This call goes through, and results in an error since the expected json data is not there.Is there a way in ab to supply the necessary json along with this request. I see there are -p and -u commands to specify an input file, but those are for puts and posts.
I realize that this answer is six years late, but I think it is worth posting since I was banging my head against a wall on a very similar issue to this, in which I was trying to load test a URL which returned only JSON data, and my solution might help other readers encountering this issue. My issue was I kept specifying the -H option when I didn't need to. That kept making the server to send back an HTTP 406 response code (Not Acceptable) to my AB request. During most of my my troubleshooting, I had also kept -T 'application/json' in the AB request as well when I didn't need it. That is only used in conjunction with PUTs or POSTs (when using the -p switch). So I removed -H, and -T, and it worked. All that said, I see those two issues here. We need to be mindful that AB uses the GET method by default.
You are constraining AB by appending extra custom headers to the
request, by using the -H option: -H 'Accept: multipart/mixed', which
might make your target server think it's an invalid request and stop the
sequence right then and there. Just don't use -H unless you have a
really good reason why.
You are using the -T option: -T 'application/json' which
only works when you specify that in conjunction with -p and you
don't have -p anywhere in your command, which you don't want to use anyway since you are sending a GET and not a PUT or a POST.
So to fix this, simply remove both the -T and -H options, and it should work. Reminder to other readers: If on Windows, enclose the URL in double-quotes whenever it contains special characters like a "&", or a "?", as in this case.
ab -n 10 -c 2 'http://phx5qa01c-02b0.stratus.phx.qa.ebay.com/.Build?format=text/html,text/plain&template-id=29b1468f-c8c3-db23-2f6f-74e112795540'

Send request to cURL with post data sourced 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. 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'

How to send line break with curl?

I've tried the following to send a line break with curl, but \n is not interpreted by curl.
curl -X PUT -d "my message\n" http://localhost:8000/hello
How can I send a line break with curl?
Sometimes you want to provide the data to be sent verbatim.
The --data-binary option does that.
Your shell is passing \ followed by n rather than a newline to curl rather than "my message\n". Bash has support for another string syntax that supports escape sequences like \n and \t. To use it, start the string with $' and end the string with ':
curl -X PUT -d $'my message\n' http://localhost:8000/hello
See ANSI-C Quoting in the Bash Reference Manual
There's a much easier way!
curl -X PUT -d $'my message\n' http://localhost:8000/hello
This will use ANSI-C Quoting to insert the newline character.
No piping, no data files. See also Sending Newlines with cURL.
The solution for someone who doesn't want to use files, and doesn't want to resort to shell escaping magic is:
curl -X POST --data-binary #- http://url.com <<EOF
line one
line two
EOF
But this is literal newlines in the post data payload, and not in form fields.
Had similar issue. While uploading a CSV file from Mac to cloud storage, new lines were being removed. After downloading it, the entire file looked like a single line. I tried adding different EOL characters \n \r \r\n with no success. Using --data-binary instead of -d solved the issue.
Btw this issue occurred only from Mac. -d worked just fine while making the call from CentOS machine. This very much looks like due to Mac's newline character. But don't feel like debugging any more.
Thanks a lot for your help.
curl -X PUT -d #filename.csv https://cloudstorage -H "content-type: text/csv"
vs
curl -X PUT --data-binary #filename.csv https://cloudstorage -H "content-type: text/csv"
(I ended up here with a slightly different question, so I'm just going to post my answer because it might help future explorers)
My solution applies to people who are sending form-style data, i.e. key/value pairs in a query string. Use the encoded line break, which is %0A, just like how an encoded space is %20. You can use http://meyerweb.com/eric/tools/dencoder/ to convert other symbols.
So if you want to set the key message to the value:
line one
another
you would send
curl --data "message=line%20one%0Aanother" http://localhost:8000/hello
A very easy way, just Shift-Enter in the console for the break. Very readable typing it in too.
curl -d "line1
line2" http-echo.com
Server gets this: line1\nline2
Do this to remove the line break:
curl -d "line1 \
line2" http-echo.com
Server gets this: line1 line2
Not an answer to your question, but I would work around it by creating a temporary file containing the message and line break, and give curl that file to work on:
curl -X PUT -d #message.txt http://localhost:8000/hello
From the manual:
If you start the data with the letter #, the rest should be a file name to read the data from, or - if you want curl to read the data from stdin. The contents of the file must already be URL-encoded. Multiple files can also be specified. Posting data from a file named 'foobar' would thus be done with --data #foobar.
I was using Sendgrid with this code (copied below) originally found here https://sendgrid.com/docs/API_Reference/Web_API_v3/index.html
\n\n worked in Gmail, but \n was ignored. I tried to double the escape and other suggestions. I also tried \r\n and that did not work in Gmail either. Note: I didn't bother to test other email clients, maybe it was a Gmail-specific problem.
curl --request POST \
--url https://api.sendgrid.com/v3/mail/send \
--header 'Authorization: Bearer YOUR_API_KEY' \
--header 'Content-Type: application/json' \
--data '{"personalizations": [{"to": [{"email": "your.email#example.com"}]}],"from": {"email": "example#example.com"},"subject": "Hello, World!","content": [{"type": "text/plain", "value": "Heya!"}]}'
Eventually I gave up looking for a solution and switched the text/plain to text/html and just used <br /> tags.
Someone suggested that Sendgrid converts plaintext to HTML if you have a tracking pixel enabled, which makes sense. Maybe the newlines were destroyed in the plaintext-to-html conversion process. I assume the client wants a tracking pixel, so decided to switch to HTML.

Resources