# FetchContent with authentication

**URL:** <https://discourse.cmake.org/t/fetchcontent-with-authentication/3293>\
**Category:** Code\
**Created:** [May 11, 2021, 11:12am UTC](https://discourse.cmake.org/t/fetchcontent-with-authentication/3293 "2021-05-11T11:12:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kinoko](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kinoko/32/1435_2.png) [@kinoko](https://discourse.cmake.org/u/kinoko)\
**Post date:** [May 11, 2021, 11:12am UTC](https://discourse.cmake.org/t/fetchcontent-with-authentication/3293/1 "2021-05-11T11:12:38Z")

</div>

I am trying to get FetchContent to download a pre-compiled library from a gitlab repository that requires authentication.  
On command line it looks like this:  
curl --header ‘PRIVATE-TOKEN: afw7f9sd87f’ [https://repo.url/api/v4/projects/mylib](https://repo.url/api/v4/projects/mylib)

I tried the following, but neither an error message, nor the file downloaded.

```
FetchContent_Declare(MyLib 
   DOWNLOAD_COMMAND /usr/bin/curl --header 'PRIVATE-TOKEN: afw7f9sd87f' https://repo.url/api/v4/projects/bla-bla
)
FetchContent_MakeAvailable(MyLib)

```

- Is it possible to use a custom command to download?
- How to halt configuration if download fails?
- is there a better way to get third party, closed source pre-compiled binaries into a build?

---

<div class="post-metadata">

**Author:** ![kinoko](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kinoko/32/1435_2.png) [@kinoko](https://discourse.cmake.org/u/kinoko)\
**Post date:** [May 11, 2021, 11:53am UTC](https://discourse.cmake.org/t/fetchcontent-with-authentication/3293/2 "2021-05-11T11:53:56Z")

</div>

Answering myself:

> Is it possible to use a custom command to download?

The custom download command works if it is a single string argument to a bash call - at least on Linux.

```
FetchContent_Declare(MyLib
    DOWNLOAD_COMMAND /usr/bin/bash -c "/usr/bin/curl --header 'PRIVATE-TOKEN ... "
)

```

> How to halt configuration if download fails?

I guess this can/has to be implemented manually.

---

<div class="post-metadata">

**Author:** ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)\
**Post date:** [May 11, 2021, 11:58am UTC](https://discourse.cmake.org/t/fetchcontent-with-authentication/3293/3 "2021-05-11T11:58:22Z")

</div>

Since CMake 3.7 there is `HTTP_HEADER` parameter you could use to set the header without resorting to overwriting `DOWNLOAD_COMMAND`.

---

<div class="post-metadata">

**Author:** ![kinoko](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kinoko/32/1435_2.png) [@kinoko](https://discourse.cmake.org/u/kinoko)\
**Post date:** [May 11, 2021, 12:00pm UTC](https://discourse.cmake.org/t/fetchcontent-with-authentication/3293/4 "2021-05-11T12:00:23Z")

</div>

Perfect, thank you. It is documented in ExternalProject  
[https://cmake.org/cmake/help/latest/module/ExternalProject.html?highlight=http\_header](https://cmake.org/cmake/help/latest/module/ExternalProject.html?highlight=http_header)

---

<div class="post-metadata">

**Author:** ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)\
**Post date:** [May 11, 2021, 9:47pm UTC](https://discourse.cmake.org/t/fetchcontent-with-authentication/3293/5 "2021-05-11T21:47:02Z")

</div>

> [@kinoko](#):
>
> The custom download command works if it is a single string argument to a bash call - at least on Linux.

The `HTTP_HEADER` keyword mentioned by @fenrir is what I’d go with to, but to shed some light on your attempt using curl, CMake doesn’t interpret single quotes like shells do. If you had surrounded the `PRIVATE-TOKEN...` part with double quotes instead of single quotes, I think it would have worked (it looks like you also have an error where you specified curl twice at the start of the command line, but I assume that was just a typo for the example here).

---

<div class="post-metadata">

**Author:** ![kinoko](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kinoko/32/1435_2.png) [@kinoko](https://discourse.cmake.org/u/kinoko)\
**Post date:** [May 12, 2021, 8:49am UTC](https://discourse.cmake.org/t/fetchcontent-with-authentication/3293/6 "2021-05-12T08:49:48Z")

</div>

Hi Craig,  
Thanks for your comment.

> part with double quotes instead of single quotes, I think it would have worked

Actually I was experimenting with double/singe/no quotes (for half a day) and I did manage to download the remote archive once. But couldn’t reproduce it.

Thanks anyhow.
