Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DELETE using CURL with encoded URL

I’m trying to make a request using CURL like this:

curl -X DELETE "https://myhost/context/path/users/OXYugGKg207g5uN/07V"  

where OXYugGKg207g5uN/07V is a hash, so I suppose that I need to encode before do this request.

I have tried curl -X DELETE --data-urlenconded "https://myhost/context/path/users/OXYugGKg207g5uN/07V"

Some ideas?

like image 985
coffee Avatar asked Oct 04 '12 20:10

coffee


People also ask

How do I delete with curl?

To make a DELETE request using Curl, you need to use the -X DELETE command-line option followed by the target URL. To pass additional headers to the HTTP server, use the -H command-line option. The "Accept: application/json" header tells the server that the client expects JSON data in response.

Does curl do URL encoding?

To help you send data you have not already encoded, curl offers the --data-urlencode option. This option offers several different ways to URL encode the data you give it. You use it like --data-urlencode data in the same style as the other --data options.

Does curl encode query parameters?

cURL can also encode the query with the --data-urlencode parameter. When using the --data-urlencode parameter the default method is POST so the -G parameter is needed to set the request method to GET.

What does curl -- data Urlencode do?

Use curl --data-urlencode ; from man curl : This posts data, similar to the other --data options with the exception that this performs URL-encoding. To be CGI-compliant, the <data> part should begin with a name followed by a separator and a content specification.


1 Answers

Since you are in a bash environment, you could encode the hash OXYugGKg207g5uN/07V before passing it to curl.

A straight-forward approach would be to use its byte representation %4f%58%59%75%67%47%4b%67%32%30%37%67%35%75%4e%2f%30%37%56

To get that, call:

echo -ne "OXYugGKg207g5uN/07V" | xxd -plain | tr -d '\n' | sed 's/\(..\)/%\1/g' 

It will give you: %4f%58%59%75%67%47%4b%67%32%30%37%67%35%75%4e%2f%30%37%56

The complete one-liner including curl in bash/zsh/sh/… would look like this:

curl -X DELETE "https://myhost/context/path/users/$(echo -ne "OXYugGKg207g5uN/07V" | xxd -plain | tr -d '\n' | sed 's/\(..\)/%\1/g')" 

and is equivalent to

curl -X DELETE "https://myhost/context/path/users/%4f%58%59%75%67%47%4b%67%32%30%37%67%35%75%4e%2f%30%37%56" 

This solution is not very pretty, but it works.
I hope you find this helpful.

like image 92
knugie Avatar answered Oct 04 '22 02:10

knugie