Curl POST request with an array of strings as a paramter - bash

I want to send a curl post request with query params. One of these params is an array of strings. An example for such a string (using Postman chrome extension):
http://www.example.com/my_rest_endpoint?start=123456&duration=11&array_param=["activity level"]
Now, how can I translate this request to a curl command? no matter what I do, I can't seem to send the last parameter, array_param to curl. I've tried escaping the quotes and/or brackets with \, tried using -i flag and other various options, all with no success.

Solution was very close to what benaryorg suggested: Brackets and spaces needs to be placed with %5B, %5D and + according to the url encoding. The quotes were escaped using \". The final result is:
http://www.example.com/my_rest_endpoint?start=123456&duration=11&array_param=%5B\"Activity+Level\"%5D".

Have you tried placing ' around the parameters instead of escaping?
Also if that does not work, you could try to escape them like this example from Wikipedia.
You can find the codes for encoding here.

Related

How to disable double escaping url in golang?

It looks like golang reverseproxy double escapes url, when making a http request,
server receives:
/id/EbnfwIoiiXbtr6Ec44sfedeEsjrf0RcXkJneYukTXa%252BIFVla4ZdfRiMzfh%252FEGs7f
expected:
/id/EbnfwIoiiXbtr6Ec44sfedeEsjrf0RcXkJneYukTXa%2BIFVla4ZdfRiMzfh%2FEGs7f
Is there a way to avoid double escaping?
Go isn't double-escaping the URL. It's constructing the URL given the values that you gave it (which means escaping the path). You've already escaped the path. Don't do that. If you want to send +, then use +. Go will escape it correctly.
If you have an escaped path already for some reason, then unescape it with url.PathUnescape() before constructing the URL.

Changing escape character for quotes

I am trying to read a CSV file which contains escaped quote values such as:
"1","unquoted text","\"quoted text\""
It seems that SuperCSV wants quotes to be quoted as
"1","unquoted text","""quoted text"""
Is there a way to change the escape character to a backslash? I've looked at the docs and not seen anything.
Just found a link to an issue logged in github: https://github.com/super-csv/super-csv/issues/14
Seems like a different CSV handler is in order.

Curl for ":" in web url

I am writing a program in bash and want to use curl to display the contents of a website. The website url is http://examplewebsite.com/example.php?pass=hello hello:world. Using:
curl http://examplewebsite.com/example.php?pass=hello hello:world
However this returns:
Couldn't resolve host 'hello:world'
The error was caused by the space between “hello” and “world” that caused bash to split the link into two different tokens, which curl interpreted as two different links.
You should at least quote the URL parameters, as Explosion Pills did.
However, it is not a good style to pass arguments directly like that, because you might end up with some characters that need escaping (space is one of those, but it seems to be handled automatically by curl).
To do this, you can use the --data-urlencode:
curl "http://examplewebsite.com/example.php" --data-urlencode "pass=hello hello:world" --get
(--data-urlencode switches the method to POST instead of GET, so you can use --get to switch back to GET)
Just wrap the URL in a string:
curl "http://examplewebsite.com/example.php?pass=hello hello:world"
Your mileage may vary as to whether this works properly or not, so you should also URL encode the value:
curl "http://examplewebsite.com/example.php?pass=hello%22hello%3Aworld"

Expand single quotes in a Net::HTTP variable?

I am using Net::HTTP to get a request from a Google API with a custom header:
req = Net::HTTP::Get.new(uri.request_uri, {'Authorization' => 'GoogleLogin auth=#{auth}'})
The #{auth} is a variable that changes each time I run the program, so I made a variable with it, but the single quotes don't expand it. I can't change the single quotes to double quotes, because Google only accepts the header with single quotes.
Is there any way to expand the variable but keep the single quotes?
However, I can't change the single quotes to double quotes, because google only accepts the header with single quotes.
So hard to believe it.
Anyway, try Kernel%sprintf or its shorter version just str % [arguments..]. It will help.

Printing printing variable in single quoted string Ruby

I am attempting to send a tweet to twitter using the twitter_oauth gem with the following code:
client.update('.# #{tweeter}, have a nice day!')
Because of the single quotes I cannot get the variable to display but the tweet will not send if single quote are not used. Does anyone have any suggestions as to how to get this to work? thanks
Just replace the ' with ", single quoted strings don't do variable substitution and the other neat things of double quoted strings. They exist because of those missing features they are faster to parse.
If the tweet doesn't work despite using " then the problem is likely that the variable tweeter contains characters that are not allowed or in some other way invalid (maybe requiring some sort of escaping, e.g. URL or XML escaping).
Have you tried the old, java-esque way:
client.update('.# ' + tweeter + ', have a nice day!')
Or using a temporary variable:
message = ".# #{tweeter}, have a nice day!"
client.update(message)

Resources