Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

translate Postman post Request with Basic Auth to mingw64 curl and powershell On Windows 10

I would like to translate a POST Request test from Postman to curl on Windows!

note: ip, username, password are illustrative for this question!

enter image description here

enter image description here

Consuming from Postman, I get 200 OK Code fine!

But, Trying to use curl from cmd is not working!

Checking on Postman the curl code enter image description here

I was trying on Powershell

curl --location --request POST 'http://10.31.7.52/BdN_SMS_CXP/api/SendSMS' --header 'Authorization: Basic bWFyaWVsYTpZMFMkaSRfViZTLjQwNio=' --header 'Content-Type: application/json' --data-raw '{"ANI" : 3007209820}'

curl --location --request POST 'http://10.31.7.52/BdN_SMS_CXP/api/SendSMS' --header 'Authorization: Basic bWFyaWVsYTpZMFMkaSRfViZTLjQwNio=' --header 'Content-Type: application/json' --data-raw '{"ANI" : 3007209820}'

checking codification/decodification is fine.

enter image description here

Now directly with user and password, according to https://stackoverflow.com/a/27442239/811293

curl --location --request POST 'http://10.31.7.52/BdN_SMS_CXP/api/SendSMS' --user 'mariela:Y0S$i$_V&S.406*' --header 'Content-Type: application/json' --data-raw '{"ANI" : 3007209820}'

Here my results. enter image description here

When I use curl of mingw64 7.71.1 version, the command is misinterpreted, because I get a 407 error, Authentication required. It's not just a powershell problem solved with Remove-item alias:curl.

I suspect you should escape some characters, and use double commas " instead of single commas '.

What considerations should I have with $, _, &, . and * characters (if I should have it with any)?

How should I write the request in powershell (or mingw64 curl) for it to work?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The generated command is not powershell curl but cmd curl :

you can just use it in cmd as it is,

enter image description here

or if you want to use powershel use it as:

enter image description here

$command= "curl --location --request POST 'http://10.31.7.52/BdN_SMS_CXP/api/SendSMS' --header 'Authorization: Basic bWFyaWVsYTpZMFMkaSRfViZTLjQwNio=' --header 'Content-Type: application/json' --data-raw '{""ANI"" : 3007209820}'"

cmd /c $command

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...