Linux Curl Save as Utf-8

CURL doesn't encode UTF-8

After a discussion with @Dekel and a suggestion coming from @Phylogenesis I could resolve the problem partially, but effectively. There are 2 ways:

  • charset=ISO-8859-1
  • encoding a file and sending as binary-data

The server could receive the correct letter using charset=ISO-8859-1. Even the response data from server showing incorrectly.

I use: curl -i -X POST -H "Content-Type: text/plain; charset=ISO-8859-1" --data-ascii "name=Administração" http://localhost:8084/ws/departments

The second way is encoding a file containing all the content you want to POST. I used Notepad++ > Format > Convert to UTF-8 (Without BOM).

Then, prompt: curl -i -X POST -H "Content-Type: text/plain; charset=UTF-8" --data-binary "@test.txt" http://localhost:8084/ws/departments.

How do I POST form data with UTF-8 encoding by using curl?

You CAN use UTF-8 in the POST request, all you need is to specify the charset in your request.

You should use this request:

curl -X POST -H "Content-Type: application/x-www-form-urlencoded; charset=utf-8" --data-ascii "content=derinhält&date=asdf" http://myserverurl.com/api/v1/somemethod

How to urlencode data for curl command?

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.

Example usage:

curl \
--data-urlencode "paramName=value" \
--data-urlencode "secondParam=value" \
http://example.com

See the man page for more info.

This requires curl 7.18.0 or newer (released January 2008). Use curl -V to check which version you have.

You can as well encode the query string:

curl --get \
--data-urlencode "p1=value 1" \
--data-urlencode "p2=value 2" \
http://example.com
# http://example.com?p1=value%201&p2=value%202

How do I save a file using the response header filename with cURL?

-J/--remote-header-name is the option you want.

You use -J in conjunction with -O, which makes curl use the file name part from the URL as its primary way to name the output file and then if there is a Content-disposition: header in the response, curl will use that name instead.

How to save both data and response headers with curl to variables

Use the -D option to write the headers to a file, then read them into a variable.

data=$(curl -D headers.txt -X GET ...)
headers=$(cat headers.txt)


Related Topics



Leave a reply



Submit