admin管理员组文章数量:1336660
Edit: I'm doing all of this in WIndows PoweShell.
I was doing the tutorial for using kubernetes (one of the ones in kubernetes.io) and it tells you to use:
export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
I had to remove the "export" for it to work in windows (also had to remove the line break for some reason, but it worked after that), so the command was:
$POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{end}}')
It worked, and if I typed:
$POD_NAME
I got the correct pod name.
The problem came from the next step where I had to use:
curl http://localhost:8001/api/v1/namespaces/default/pods/$POD_NAME:8080/proxy/
Here, it was unable to resolve the url, while if I just replaced $POD_NAME with the text in the variable, it worked just fine.
I was wondering if there is a way to make it work (as in making the variable name be substituted by it's content when interpreting the command).
Not sure if it helps, but this is the error message:
curl : { "kind": "Status", "apiVersion": "v1", "metadata": {}, "status": "Failure", "message": "pods \"proxy\" not found", "reason": "NotFound", "details": { "name": "proxy", "kind": "pods" }, "code": 404 }
En línea: 1 Carácter: 1
+ curl http://localhost:8001/api/v1/namespaces/default/pods/$POD_NAME:8 ...
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
Edit: I've tried a little bit more, and it seems the problem is that powershell interprets $POD_NAME:8080
as if it was the name of the variable (instead of just $POD_NAME
and considering :8080
as part of the string that's not part of the variable). Still I would like to know id there's a way to do this instead of having to create a new string by adding the first part (before the 8080
) to the second part.
Edit: I'm doing all of this in WIndows PoweShell.
I was doing the tutorial for using kubernetes (one of the ones in kubernetes.io) and it tells you to use:
export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
I had to remove the "export" for it to work in windows (also had to remove the line break for some reason, but it worked after that), so the command was:
$POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{end}}')
It worked, and if I typed:
$POD_NAME
I got the correct pod name.
The problem came from the next step where I had to use:
curl http://localhost:8001/api/v1/namespaces/default/pods/$POD_NAME:8080/proxy/
Here, it was unable to resolve the url, while if I just replaced $POD_NAME with the text in the variable, it worked just fine.
I was wondering if there is a way to make it work (as in making the variable name be substituted by it's content when interpreting the command).
Not sure if it helps, but this is the error message:
curl : { "kind": "Status", "apiVersion": "v1", "metadata": {}, "status": "Failure", "message": "pods \"proxy\" not found", "reason": "NotFound", "details": { "name": "proxy", "kind": "pods" }, "code": 404 }
En línea: 1 Carácter: 1
+ curl http://localhost:8001/api/v1/namespaces/default/pods/$POD_NAME:8 ...
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
Edit: I've tried a little bit more, and it seems the problem is that powershell interprets $POD_NAME:8080
as if it was the name of the variable (instead of just $POD_NAME
and considering :8080
as part of the string that's not part of the variable). Still I would like to know id there's a way to do this instead of having to create a new string by adding the first part (before the 8080
) to the second part.
1 Answer
Reset to default 0I was doing the tutorial for using kubernetes (one of the ones in kubernetes.io) and it tells you to use:
export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
I had to remove the "export" for it to work in windows (also had to remove the line break for some reason, but it worked after that), so the command was: $POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{end}}')
It worked, and if I typed: $POD_NAME I got the correct pod name. The problem came from the next step where I had to use:
curl http://localhost:8001/api/v1/namespaces/default/pods/$POD_NAME:8080/proxy/
Here, it was unable to resolve the url, while if I just replaced $POD_NAME with the text in the variable, it worked just fine. I was wondering if there is a way to make it work (as in making the variable name be substituted by it's content when interpreting the command). Not sure if it helps, but this is the error message:
curl : { "kind": "Status", "apiVersion": "v1", "metadata": {}, "status": "Failure", "message": "pods \"proxy\" not found", "reason":
"NotFound", "details": { "name": "proxy", "kind": "pods" }, "code": 404 } En línea: 1 Carácter: 1 + curl http://localhost:8001/api/v1/namespaces/default/pods/$POD_NAME:8 ... + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.
I see what's happening here. When you use $POD_NAME in the curl command, it isn't getting replaced by its value. This issue often arises from how the shell handles variable interpolation.
In PowerShell, which it looks like you might be using, you need to use different syntax for variable interpolation. Here’s how you can make it work:
$POD_NAME = $(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{end}}')
curl "http://localhost:8001/api/v1/namespaces/default/pods/$POD_NAME:8080/proxy/"
本文标签: powershellHow to make curl in Windows use a variable as part of a pathStack Overflow
版权声明:本文标题:powershell - How to make curl in Windows use a variable as part of a path - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742419557a2471380.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
%POD_NAME%
,%$POD_NAME%
,$%POD_NAME%
and all of them with double %, but I can't seem to make any of them work. Maybe I'm doing it wrong, it looks like this:curl http://localhost:8001/api/v1/namespaces/default/pods/%POD_NAME%:8080/proxy
– Pablo Domínguez Commented Nov 19, 2024 at 15:34