2
votes

I'm using a bat file that I didn't write, and supposedly all you have to do is put in a few pieces of info (api key, domain, & responder ID) and it should work, the beginning of the file looks like this (I've obfuscated some stuff):

@echo off
setlocal enabledelayedexpansion
goto :main

:main
setlocal

    set domain=https://*****.*******.com
    set apiKey=9************XP
    set responderId=3*******8

The first time I tried executing the file from cmd I got an error, "Something went wrong while making the request, response_code is " with no actual response code. I did some digging and then ran copy.bat any_optional_parameters > copy.log instead, and got some slightly more useful information:

% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 40 100 40 0 0 134 0 --:--:-- --:--:-- --:--:-- 134 % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 40 100 40 0 0 111 0 --:--:-- --:--:-- --:--:-- 111 curl: (3) Host name '%http_code https' contains bad letter % Total % Received % Xferd Average Speed Time Time
Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- 0:00:01 --:--:-- 0curl: (6) Could not resolve host: from 0 0 0 0 0 0 0 0 --:--:-- 0:00:01 --:--:-- 0curl: (6) Could not resolve host: number curl: (3) Port number ended with '"'
0 0 0 0 0 0 0 0 --:--:-- 0:00:01 --:--:-- 0curl: (6) Could not resolve host: from 0 0 0 0 0
0 0 0 --:--:-- 0:00:01 --:--:-- 0curl: (6) Could not resolve host: number curl: (3) [globbing] unmatched close brace/bracket in column 123 curl: (3) Host name '%http_code https' contains bad letter

I read here that the '^' character acts as an escape in bat files, but in that instance they were adding them, and didn't address what to do if one was in there by mistake (which is what I suspect in this case). I was able to find a couple of those carats in the bat file, like so:

for /f "tokens=*" %%g in ( 'curl -u !apiKey!:X -H "Content-Type: application/json" -X GET --write-out " %%{http_code}" !domain!/contacts.json?query^=phone+is+!num!' )

Is that maybe what's breaking this for me? If not, any other ideas? I did look up the curl errors but the explanations were somewhat generic for a noob like me. Thanks in advance for any pointers!

1
// , I used this to help out a colleague. Sometimes it occurs when backslashes (``) used in a Bash example get copied into Powershell or whatnot in a single line.Nathan Basanese

1 Answers

0
votes

The element %%{http_code} will be resolved to the literal %{http_code} since % is the exception for escaping - the escape for % is % itself, not ^.

Now since I've no idea what you expect this to be resolved to, I'm reduced to guessing.

If you want the actual content of the environment variable http_code to be substituted, then the correct syntax is {%http_code%} to yield {0.0.0.0}