1

I am trying to use nodeJS request library to issue a POST request to purge content associated with a certain surrogate key via Fastly API. The POST request would look something like this:

POST /service/SU1Z0isxPaozGVKXdv0eY/purge
Content-Type: application/json
Accept: application/json
Fastly-Key: YOUR_FASTLY_TOKEN
Fastly-Soft-Purge: 1
Surrogate-Key: key_1 key_2 key_3

I tried to do this in node.JS two different ways.

The first:

    // perform request to purge
    request({
        method: `POST`,
        url: `/service/${fastly_service_id}/purge${surrogateKeyArray[i]}`, 
        headers: headers,
    }, function(err, response, body) {
        // url was not valid to purge
        if (err) {
            console.log("is there an err???")
            console.log(err)
        }
    })
    }

I get, Error: Invalid URI: /service/<fastly_Service_id>/purge/<surrogate_key>

I double checked my surrogate key via curl -s -I -H "Fastly-Debug: 1" <URL corresponding to surrogate key> | grep surrogate-key

And it returns the same surrogate-key used in my code.

In the second attempt, I tried:

    // perform request to purge
    request({
        method: `POST /service/${fastly_service_id}/purge${surrogateKeyArray[i]}`,
        headers: headers,
    }, function(err, response, body) {
        // url was not valid to purge
        if (err) {
            console.log("is there an err???")
            console.log(err)
        }
    })
    }

I get the error, Error: options.uri is a required argument

maddie
  • 1,854
  • 4
  • 30
  • 66

1 Answers1

2

I'm unfamiliar with node and the code involved to make a HTTP request successfully, but from what I can see of the code you have provided it would appear the error is related to the fact you've not provided a fully qualified domain (e.g. you're missing https://api.fastly.com before the path).

Unless of course this is configured elsewhere in your code and it's just not visible here.

Also make sure that inbetween /purge and ${surrogateKeyArray[i]} you include a / divider (I show this in my example code below).

So with this in mind I would suggest trying:

request({
    method: `POST`,
    url: `https://api.fastly.com/service/${fastly_service_id}/purge/${surrogateKeyArray[i]}`, 
    headers: headers,
    }, function(err, response, body) {
        if (err) {
            console.log(err)
        }
    })
}
Integralist
  • 495
  • 3
  • 7