Using CURL Windows to post json [closed] - windows

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 1 year ago.
Improve this question
curl.exe -X POST "https://api.inxpress.com/imcs_gateway/c_1/" -H "Content-Type: application/json" -D^
{^
"request_id":"pm77",^
"request_time":"2020-12-24",^
"account":"accountname",^
"password":"apikey",^
"gateway":"UK",^
"carrierCode":"C_1",^
"origin":{^
"city":"ROCHDALE",^
"postal_code":"OL16 5DB",^
"country":"GB"^
},^
"destination":{^
"city":"Bellville",^
"postal_code":"7530",^
"country":"ZA"^
},^
"product_code":"P",^
"content_type":"WPX",^
"weight_unit":"kg",^
"dimension_unit":"cm",^
"currency":"GBP",^
"insured_value":"3.00",^
"dutiable":true,^
"commercial_value":"5.00",^
"pieces":[^
{^
"name":"Piece 1",^
"weight":5.0,^
"height":8.0,^
"depth":12.0,^
"width":12.0,^
"quantity":1,^
"custom_value":10.0^
}^
]^
}
I'm trying to execute this batch file in windows but I must have a formatting problem with the data as the output is like this
curl: (6) Could not resolve host: request_id
curl: (6) Could not resolve host: request_time
curl: (6) Could not resolve host: account
curl: (6) Could not resolve host: password
curl: (6) Could not resolve host: gateway
curl: (6) Could not resolve host: carrierCode
curl: (3) [globbing] unmatched brace in column 8
curl: (6) Could not resolve host: city
curl: (6) Could not resolve host: postal_code
curl: (6) Could not resolve host: country
curl: (3) [globbing] unmatched close brace/bracket in column 1
curl: (3) [globbing] unmatched brace in column 13
curl: (6) Could not resolve host: city
curl: (6) Could not resolve host: postal_code
curl: (6) Could not resolve host: country
curl: (3) [globbing] unmatched close brace/bracket in column 1
curl: (6) Could not resolve host: product_code
curl: (6) Could not resolve host: content_type
curl: (6) Could not resolve host: weight_unit
curl: (6) Could not resolve host: dimension_unit
curl: (6) Could not resolve host: currency
curl: (6) Could not resolve host: insured_value
curl: (6) Could not resolve host: dutiable
curl: (6) Could not resolve host: commercial_value
curl: (3) [globbing] bad range specification in column 9
curl: (3) [globbing] unmatched brace in column 1
curl: (6) Could not resolve host: name
curl: (6) Could not resolve host: weight
curl: (6) Could not resolve host: height
curl: (6) Could not resolve host: depth
curl: (6) Could not resolve host: width
curl: (6) Could not resolve host: quantity
curl: (6) Could not resolve host: custom_value
curl: (3) [globbing] unmatched close brace/bracket in column 1
curl: (3) [globbing] unmatched close brace/bracket in column 1

Capital -D option is not in my curl at least, maybe you meant -d aka --data?
Anyway the problem seems to be that curl understands your request as multiple command-line parameters rather than a request string for -D.
At least you have to add " right after -D and a space, and one at the end, and escape all " in the request as ^".
But even that may not be enough - then the easiest option is to read the request from a file with curl --data #filename-of-request.txt
If you want to keep everything in one file, try sending request data through a stream like this:
(
echo line 1 of your request
echo line 2 of your request
...................
echo line N of your request
) | curl --data #- OTHER-CURL-PARAMETERS-HERE
here:
echo something outputs something to stdout stream (default - on screen)
echo simply outputs its commandline so it's much more tolerant to special characters in the command line, but you may still need to escape some of them with ^
| (pipe) redirects echo's stdout into curl's stdin stream
( ... ) make cmd bunch several echo output together
please note: the output will include line breaks
instead you can try bunching up all the text into one echo with ^-at-line-end trick you used
--data #file makes curl read POST data from file
#- specifies stdin as file which is where echo output went

Related

Calling curl command from Ruby's system method

I'm using curl command inside logstash exec plugin to post a message to a stride group. The plugin documentation stated that they were using ruby system method to execute the command, so I'm trying to run it in my ruby IRB.
Escaping the double quotes with backslash character is giving error The request body cannot be parsed as valid JSON. Here is the full error
irb(main):050:0' --inf-ruby-2f3827a9-23243-13517-726000--
curl: (6) Couldn't resolve host 'first'
curl: (3) [globbing] unmatched close brace/bracket in column 9
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 165 0 87 100 78 60 54 0:00:01 0:00:01 --:--:-- 60
{
"statusCode": 400,
"message": "The request body cannot be parsed as valid JSON"
}=> true
I tried swapping double quotes with single quotes and using full double quotes everywhere. Nothing is working.
system("curl -X POST -H 'Content-Type: application/json' -H 'Authorization: Bearer blah-blah' -d '{\"body\":{\"version\":1,\"type\":\"doc\",\"content\":[{\"type\":\"paragraph\",\"content\":[{\"type\":\"text\",\"text\":\"My first message!\"}]}]}}' --url 'https://api.atlassian.com/site/blah-blah/conversation/blah-blah/message'")
Is there any way to make this work?
EDIT: I have tried running the cURL command in the terminal and it is working fine.
Besides the following answers on StackOverflow, NetHTTP has notoriously poor documentation but can be used to post what you're interested in.

encode curl GET request and construct the URL

i've the following example dynamic generated url for GET request
http://api.jwplatform.com/v1/videos/create?api_format=json&api_key=dgfgfg&api_nonce=554566&api_timestamp=1500296525&custom.videoId=905581&description=تقليد بداية العام&downloadurl=http://media.com/media/mymedia.mp4&sourceformat=mp4&sourcetype=url&sourceurl=http://media.com/media/mymedia.mp4&title=الغطس بالمياه الباردة.. تقليد بداية العام&api_signature=5cd337198ead0768975610a135e2
which is include the following vars
api_key=
api_nonce=
api_timestamp=
custom.videoId=
description=
downloadurl=
sourceurl=
title=
api_signature=
sourceformat=mp4
sourcetype=url
i;m tring to get send curl GET command and get the response back i always failed for 2 reason ,
the urls in the request should be encoded utf8
and the 2nd one the i always get curl: (6) Couldn't resolve host for
each vars like its not taking the url as one url and break it into
different calls for example
curl: (6) Couldn't resolve host 'بداية'
Input domain encoded as `ANSI_X3.4-1968'
Failed to convert العام&downloadurl=http: to ACE; System iconv failed
getaddrinfo(3) failed for العام&downloadurl=http::80
curl -X -G -v -H "Content: agent-type: application/x-www-form-urlencoded" http://api.jwplatform.com/v1/videos/create?api_format=json&{vars}
and tips how i can reach this and construct the curl command in the right format

elasticsearch xpack - Can’t change default password

I installed Xpack for elasticsearch, and tried to to change the password as it says here:
https://www.elastic.co/guide/en/x-pack/current/security-getting-started.html
Running this:
curl -XPUT -u elastic localhost:9200/_xpack/security/user/elastic/_password -H Content-Type: application/json -d {"password" : "elasticpassword"}
is getting me:
{"error":{"root_cause":[{"type":"json_parse_exception","reason":"Unexpected character ('p' (code 112)): was expecting double-quote to start field name\n
at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput#4a142671; line: 1, column: 3]"}],"type":"json_parse_exception","reason":"Unexpected character ('p' (code 112)): was expecting double-quote to start field name\n
at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput#4a142671; line: 1, column: 3]"},"status":500}curl: (6) Could not resolve host: application
curl: (3) Bad URL, colon is first character
curl: (3) [globbing] unmatched close brace/bracket in column 16
or, running this:
curl -XPUT -u elastic "localhost:9200/_xpack/security/user/elastic/_password" -H "Content-Type: application/json -d {"password" : "elasticpassword"}
gets me this:
{"error":{"root_cause":[{"type":"not_x_content_exception","reason":"Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes"}],"type":"not_x_content_exception","reason":"Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes"},"status":500}
I can't seem to get it, no matter what combination of " or ' I'm using. Please help!
Thanks
This is general problem of Windows users. You can use Sense Chrome Plugin or Kibana Dev Tools instead.
curl -XPUT -u elastic localhost:9200/_xpack/security/user/elastic/_password -H "Content-Type: application/json" -d "{\"password\" : \"elasticpassword\"}"
Please check the elastic developer response about this situation:
https://discuss.elastic.co/t/index-a-new-document/35281/8

CMD/Powershell: Commands not working with cURL. Cygwin works

I'm trying to get this tutorial to work in CMD and Windows Powershell (Windows 7 64): http://www.elasticsearchtutorial.com/elasticsearch-in-5-minutes.html
The first command already doesn't work in CMD:
curl -XPUT 'http://localhost:9200/blog/user/dilbert' -d '{ "name" : "Dilbert Brown" }'
I get the following error message:
curl: (1) Protocol 'http not supported or disabled in libcurl
curl: (6) Could not resolve host: name
curl: (7) Failed to connect to port 80: Connection refused
curl: (6) Could not resolve host: Dilbert Brown
curl: (3) [globbing] unmatched close brace/bracket in column 1
In Windows Powershell I get:
{"error":"MapperParsingException[failed to parse]; nested: JsonParseException[Unrecognized token 'Dilbert': was expecting ('true', 'false' or 'null')\n at [Source: [B#132b73b; line: 1, column: 33]]; ","status":400}curl: (3) [globbing] unmatched close brace/bracket in column 7
The same command works without any problems in Cygwin. How would I have to write the command to make it work in CMD and Windows Powershell?
In PowerShell, the command is being called correctly, from a parsing perspective.
In cmd.exe, the single quotes are not valid. You will need to use double quotes to surround values.
curl -XPUT "http://localhost:9200/blog/user/dilbert" -d "{ \"name\" : \"Dilbert Brown\" }"

How to specify command parameters in one variable?

In the test script so many times I use the command "curl". In order to optimize the code, I want options of "curl" to carry out in a global variable.
I read the terms of use of "curl", and it says that to pass a parameter that contains spaces must be to frame it in single quotes.
But it is not working.
$ curl_options="-i -L -k -S --connect-timeout 30 --user-agent 'Opera/9.80 (Windows NT 6.1; WOW64) Presto/2.12.388 Version/12.14'"
$ curl $curl_options "http://google.com"
Output Result:
curl: (6) Couldn't resolve host'' Opera '
curl: (6) Couldn't resolve host '(Windows'
curl: (6) Couldn't resolve host 'NT'
curl: (6) Couldn't resolve host '6 .1; '
curl: (6) Couldn't resolve host 'WOW64)'
curl: (6) Couldn't resolve host 'Presto'
curl: (6) Couldn't resolve host 'Version'
In bash, you should use an array. This way, you don't need to worry whether a space in the string is part of an option, or separating two options:
curl_options=( ... )
curl_options+=( "--user-agent" "Opera/9.80 (Windows NT 6.1; WOW64) Presto/2.12.388 Version/12.14")
curl "${curl_options[#]}" "http://google.com"
If you can't use arrays (e.g., they aren't available in the shell you are using), you'll have to fall back to using eval:
$ curl_options="-i -L -k -S --connect-timeout 30 --user-agent 'Opera/9.80 (Windows NT 6.1; WOW64) Presto/2.12.388 Version/12.14'"
$ eval "curl $curl_options http://google.com"
This isn't ideal, since you need to be very careful about how you set the value of curl_options, because eval has no knowledge about what the value represents. The shell merely interpolates the value into the string passed to eval, and eval executes it. Typos can have unintended consequences.

Resources