1

I am trying to test the Sumo Logic API by updating the information of my collector. The second curl command is the one that is causing the issue 'curl: (55) Failed sending PUT request'. It works in my terminal but not in the bash script.

#!/bin/bash

readonly etag=$(curl -u '<accessId>:<accessKey>' -I -X GET https://api.sumologic.com/api/v1/collectors/<id> | grep -Fi etag | awk '{print $2}' | tr -d \''"\')

echo ${etag}

curl -vvv -u '<accessId>:<accessKey>' -X PUT -H "Content-Type: application/json" -H "If-Match: \"${etag}\"" -T updated_collector.json https://api.sumologic.com/api/v1/collectors/<id>

set -x

The first curl command is assigned to the variable called 'etag' which stores the necessary etag. The etag is used in the second curl command to make a request to update the information stored in the 'updated_collector.json'. The updated_collector.json file is not the issue as I have successfully updated the information via the terminal with it. I suspect the content-type is not being sent in the header because someone ran the script on their end and it was not showing that information with the -vvv tag.

Here you can find the Sumo Logic Collector API Methods and Examples from which I got the curl commands to test the API: https://help.sumologic.com/APIs/Collector-Management-API/Collector-API-Methods-and-Examples

Update: I retieved the etag and then ran the second command in a bash script. I manually inserted the etag into the ${etag} portion of the second curl command. I then ran the script and it worked. Therefore, the etag variable isn't correctly formatted inside the second curl command. I do not know how to fix this.

  • 1
    Put the `set -x` line just below `#!...` and re-run, it should list out all the commands so you can see what went wrong. – msbit Jun 21 '21 at 23:43
  • @msbit it outputs the same information. another update, I have removed | tr -d \''"\' from the first curl that gets the etag and it runs. However, now I get a different error: curl: (92) HTTP/2 stream 0 was not closed cleanly: PROTOCOL_ERROR (err 1) – CubeDomination Jun 21 '21 at 23:51
  • 1
    Have you tried using it without the curly braces. `\”$etag\”` – abhishek phukan Jun 22 '21 at 00:28
  • @abhishekphukan I removed the curly braces yes. I forgot to mention in the previous comment that is also part of what I did and now I get that error curl(92). – CubeDomination Jun 22 '21 at 00:44
  • 1
    `echo ${etag}` is not a good way to see what's in the variable; try `printf '%s' "${etag}" | xxd` to get a better idea what's really there. – Gordon Davisson Jun 22 '21 at 01:09
  • @GordonDavisson that's not my issue. – CubeDomination Jun 22 '21 at 02:34
  • Are you sure? It sounds like there's something weird in the `etag` variable, so knowing exactly what's there would clarify that. Also, compare the trace output from `set -x` to what you're doing when you run it manually. Do you see any difference? What happens if you copy & paste the `set -x` output into the command line and run that? – Gordon Davisson Jun 22 '21 at 03:38
  • Run the script with `bash -x script.sh`. It will print the curl command that gets executed. That will help in debugging. Please paste the output here if you are not able to debug – abhishek phukan Jun 22 '21 at 06:24
  • @abhishekphukan I ran the command and got some useful output. Here is what I have noticed. The output is like this: `"' -T updated_collector.json https://api.sumologic.com/api/v1/collectors/ -X PUT -H 'Content-Type: application/json' -H 'If-Match: "bc5b1699c87128b03f7dcbe7a06bdb2c` the statement should be like this: `curl -vvv -u accessID:accessKey -X PUT -H 'Content-Type: application/json' -H 'If-Match: "4e50acbaead3f486e299b2671f250df1"' -T updated_collector.json https://api.sumologic.com/api/v1/collectors/` I have ommited some details as the response is long. – CubeDomination Jun 22 '21 at 16:39
  • @GordonDavisson see comment above. sorry for the formatting. – CubeDomination Jun 22 '21 at 16:39
  • 1
    There's a carriage return at the end of the `etag` variable (because the result from the first `curl` request is coming back in "network" format, with CRLF line endings). I thought it might be something like this, which is why I suggested `printf '%s' "${etag}" | xxd` (it'd show as "0d" at the end of the hex dump). To remove it, add `\r` to the list of characters to remove with `tr -d`. See [this question](https://stackoverflow.com/questions/30582516/bash-script-grep-using-variable-fails-to-find-result-that-actually-does-exist) for more info. – Gordon Davisson Jun 22 '21 at 18:06

1 Answers1

0

The issue was partially the syntax but after fixing that, I was still getting an error. "If-Match: \"${etag}\" in my command should be "If-Match: ${etag}" instead. I had to add the --http1.1 flag for it to work. I'm sure this is a sumo logic issue. I am able to execute GET requests no problem using http2.0.