powershell - How to make curl in Windows use a variable as part of a path - Stack Overflow

Edit: I'm doing all of this in WIndows PoweShell.I was doing the tutorial for using kubernetes (o

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.

Share Improve this question edited Nov 21, 2024 at 20:52 Pablo Domínguez asked Nov 19, 2024 at 15:16 Pablo DomínguezPablo Domínguez 113 bronze badges 1
  • I've tried %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
Add a comment  | 

1 Answer 1

Reset to default 0

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.

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/"

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742419557a4440406.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信