User agent with fetch tool

Using the following code to try and update my Google DDNS record with current IP, everything seems to work correctly with communicating with Google but Google returns a “badagent” error since the request needs to send a valid user agent to be accepted by Google. How can I set and/or customize a user agent that is sent with the fetch tools URL request?

# Variables
# Username and password are the long cryptic ones Google Domains provides,
# not your regular Google account ones
:local GoogleDNSUsername "gbML26UKGdjm7Oz1"
:local GoogleDNSPassword "ASmwlKjCAkEywaZC"
:local hostName "test.twinstar5.com"
:local currentIP ""
:local setResults ""
:local previousIP ""

# Script
:set currentIP [/ip cloud get public-address]
:set previousIP [:resolve "$hostName"]

:if ($currentIP != $previousIP) do={
:do {
/tool fetch url="https://$GoogleDNSUsername:$GoogleDNSPassword@domains.google.com/nic/update?hostname=$hostName&myip=$currentIP" mode=https dst-path=GoogleDNS.txt
:set setResults [/file get GoogleDNS.txt contents];
:log info ("GoogleDNS said this: $setResults")
} on-error={ 
:log error ("GoogleDNS: script failed to set new IP address") 
}
}

I’m in the same boat and get the same error, did you manage to find a fix for this?

Cheers,
Tugsynz

No, unfortunately. I still don’t believe there is a way to add or customize a user agent for requests, although I have not tried to do it again on the latest versions of Router OS.

/ tool fetch http-header-field="User-Agent: Mozilla/4.0" ...

/tool fetch url=“http://example.com/” http-header-field=“User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0”

HTTP_USER_AGENT: Mikrotik/6.x Fetch, Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0
Seems to be no chance to replace, just add to “Mikrotik/6.x Fetch”

Sorry to resurrect an old thread but how did you get it to append? I used that exact line and it came back as ‘Mikrotik/6.x Fetch’ I’m on 6.47.1


Command:
/tool fetch url="http://postman-echo.com/get" http-header-field="User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0"

Results:
{"args":{},"headers":{"x-forwarded-proto":"http","x-forwarded-port":"80","host":"postman-echo.com","x-amzn-trace-id":"Root=1-5f1f0b8a-fc7505a8e28e38802ca59cf8","user-agent":"Mikrotik/6.x Fetch"},"url":"http://postman-echo.com/get"}

http://postman-echo.com/get just returns what it gets

Just use http-header-field="user-agent: useragent"

> :put ([/tool fetch url="http://postman-echo.com/get" http-header-field="user-agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0" output=user]->"data")
      status: finished
  downloaded: 0KiBC-z pause]
        data: { "args": {}, "headers": { "x-forwarded-proto": "http", "x-forwarded-port": "80", "host": "postman-echo.com", "x-amzn-trace-id": "Root=1-6511a09c-59322be6549949c957d1ae2c", "user-agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0" }, "url": "http://postman-echo.com/get" }