How to urlencode curl messages?

i have script got error when curl message . i think have some problem with character "space" or other because when i tried skip space , script running normal.

this is my script :

#!/bin/env bash 
hostname="$(hostname)" now="$(date +'%d%h%y_%H.%M.%S')"  while IFS= read -r line;  do curl -X GET "http://x.x.x.x:5000/submit_fajar.php?msg=\"${line}+$line+$hostname\"" done < <(cat test.txt | strings | awk -F'|' '$4 > 100{print $1 "_" $2 "_" "please check have_many_error respond_from server" "_" $3 "_" "count", $4}')

when running got error :

+ curl -X GET 'http://x.x.x.x:5000/submit_fajar.php?msg="2019-07-20_19:42_please check have_many_error respond_from server_server busy_count 176+2019-07-20_19:42_please check have_many_error respond_from server_server busy_count 176+server"' 
<html><body><h1>400 Bad request</h1> Your browser sent an invalid request. </body></html> + IFS= + read -r line

please help how to url encode my messages to curl. Thanks.

Here is an easy PHP way, as a high level example:

<?php
$encoded  = urlencode($string_to_encode);
$results = file_get_contents($encoded);

Cheers.

If you want this in bash, you could define a function to call:

function encode_url () {
   printf "%s" "$*" | perl -pe 's/([^0-9a-zA-Z_])/sprintf("%%%02x",ord($1))/ge'
}

The in your code you can write:

my_encoded_message="$(encode_url "${my_message}")"

Does that give you what you need? For my_message="Hello world" it puts the string Hello%20world in my_encoded_message

Kind regards,
Robin