# LUA HTTP Request with Content-Type: application/x-www-form-urlencoded

**URL:** <https://forum.farmbot.org/t/lua-http-request-with-content-type-application-x-www-form-urlencoded/9223>\
**Category:** Software\
**Created:** [June 28, 2024, 9:42pm UTC](https://forum.farmbot.org/t/lua-http-request-with-content-type-application-x-www-form-urlencoded/9223 "2024-06-28T21:42:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![farmS1m](https://forum.farmbot.org/letter_avatar_proxy/v4/letter/f/f04885/32.png) [@farmS1m](https://forum.farmbot.org/u/farmS1m)\
**Post date:** [June 28, 2024, 9:42pm UTC](https://forum.farmbot.org/t/lua-http-request-with-content-type-application-x-www-form-urlencoded/9223/1 "2024-06-28T21:42:17Z")

</div>

Hi all,

I’m currently in the process of coding a Lua sequence to fetch data from my Netatmo Weather Station ([https://www.netatmo.com/smart-rain-gauge](https://www.netatmo.com/smart-rain-gauge)).

Unfortunately it is a bit tricky to implement, since the requests have to be made with a proper oauth accesstoken.

As long as I do have a valid access token I’m able to fetch data from the Netatmo API.

Since the Netatmo accesstoken at somepoint expires, I need to fetch a new access token via the refreshtoken. See documentation: [Netatmo Connect | Authentication](https://dev.netatmo.com/apidocumentation/oauth#refreshing-a-token)

For this api call I have to send a POST Request. Unfortunately this is the problematic part I’m currently stuck.

I’m unable to set the http content-type to:

```auto
Content-Type: application/x-www-form-urlencoded;charset=UTF-8

```

**Question: Could it be that the http function only supports application/json?**

# My code

should fetch a new accesstoken in case the regular requests no more deliver data:

```auto
device_id = "xyz"
request_url = "https://api.netatmo.com/api/getstationsdata?device_id=" .. device_id .. "&get_favorites=false"

netatmo_accesstoken = env("netatmo_accesstoken")

-- fake access token to force, wrong token...
--netatmo_accesstoken = "sdfasfasdf"

netatmo_refreshtoken = env("netatmo_refreshtoken")

netatmo_clientid = env("netatmo_clientid")
netatmo_clientsecret = env("netatmo_clientsecret")

toast(netatmo_refreshtoken)
toast(netatmo_accesstoken)
toast(netatmo_clientid)
toast(netatmo_clientsecret)

env("netatmo_rain_mm_24h")

toast("Getting data from " .. request_url)
response, error = http({
    url=request_url,
    headers={
    Authorization='Bearer ' .. netatmo_accesstoken,
    Accept="application/json"
  }
})
if error then
  send_message("info", "Unknown error: " .. inspect(error))
else
  -- The `response` object has three properties:
    -- `status`: Number - Response code. Example: 200.
    -- `body`: String - Response body. Always a string.
    -- `headers`: Map<String, String> - Response headers.
end

toast(response.status)
toast(inspect(response.headers))

-- toast(payload)

if response.status > 400 then

  --send_message("warning", "Netatmo api response was error 401, accesstoken probably expired. Fetching a new one.")
  response, error = http({
      url='https://api.netatmo.com/oauth2/token',
      method="POST",
      headers={
        Content-Type="application/x-www-form-urlencoded;charset=UTF-8"
      },
      body="client_id=xyz&client_secret=xyz&grant_type=refresh_token&refresh_token=xyz"
  })
  if error then
    -- The `error` object is reserved for non-HTTP errors.
    -- Example: missing URL.
    -- `error` will be `nil` if no issues are found.
    send_message("info", "Use of refreshtoken failed, error: " .. inspect(error))
  else

  end
  toast(response.status)
  toast(response.headers)
  toast(response.body)
  refreshtoken_response = json.decode(response.body)

  send_message("info", "Refreshtoken response: " .. refreshtoken_response)
  env("netatmo_refreshtoken", refreshtoken_response.refresh_token)
  env("netatmo_accesstoken", refreshtoken_response.access_token)

else
  toast("Access token is still valid")
end

netatmo_data = json.decode(response.body)

rain_mm_24h = netatmo_data.body.devices[1].modules[3].dashboard_data.sum_rain_24

toast("Netatmo messured rain in the last 24hours: " .. tostring(rain_mm_24h) .. "mm")
send_message("info", "Netatmo messured rain in the last 24hours: " .. tostring(rain_mm_24h) .. "mm")

-- Store it in Variable
env("netatmo_rain_mm_24h", tostring(rain_mm_24h))

```

I’ve tried to set the Content-Type in the headers… But this produces a Lua Error:

`Failed to execute command: Lua code is possibly invalid: [{51, :luerl_parse, [~c"syntax error before: “, ~c”‘=’"]}]`

 ![image](https://forum.farmbot.org/uploads/default/original/2X/1/188ff49a6835308128197f97cda6f10e78af18ba.png)

# For reference:

Fetching a new accesstoken via Postman works:

 ![image](https://forum.farmbot.org/uploads/default/original/2X/3/3a5d5b5afe8098c560b5c977b1d46e0e3161ceea.png)

Fetching it in Postman with application/json will as expected fail too:

 ![image](https://forum.farmbot.org/uploads/default/original/2X/1/1582bf4989d76710777131fcab954c2615b01843.png)

Thanks for some inputs in advance 🤓

---

<div class="post-metadata">

**Author:** ![farmS1m](https://forum.farmbot.org/letter_avatar_proxy/v4/letter/f/f04885/32.png) [@farmS1m](https://forum.farmbot.org/u/farmS1m)\
**Post date:** [June 29, 2024, 12:06am UTC](https://forum.farmbot.org/t/lua-http-request-with-content-type-application-x-www-form-urlencoded/9223/2 "2024-06-29T00:06:40Z")

</div>

😃 😃 😃  
After lots of try and error - searching through the docs I was able to find the solution.

Declaring the header differently and Lua accepted the code 🙂  
Inspired from sample code: [Identify Plants | FarmBot Developer Documentation](https://developer.farm.bot/v15/lua/examples/identify-plants.html)

```auto
  headers = {}
  headers["Content-Type"] = "application/x-www-form-urlencoded;charset=UTF-8"

    send_message("error", "Netatmo api response was error " .. tostring(response.status) .. ", accesstoken probably expired. Fetching a new one.")
    response, error = http({
        url='https://api.netatmo.com/oauth2/token',
        method="POST",
        headers=headers,
        body="client_id=" .. netatmo_clientid .. "&client_secret=" .. netatmo_clientsecret .. "&grant_type=refresh_token&refresh_token=" .. netatmo_refreshtoken
    })

```

I’m still a bit curious why my initial Lua code did not work. Is it because of the dash in “Content-Type”?

---

<div class="post-metadata">

**Author:** ![jsimmonds](https://forum.farmbot.org/user_avatar/forum.farmbot.org/jsimmonds/32/1470_2.png) [@jsimmonds](https://forum.farmbot.org/u/jsimmonds)\
**Post date:** [June 29, 2024, 12:57am UTC](https://forum.farmbot.org/t/lua-http-request-with-content-type-application-x-www-form-urlencoded/9223/3 "2024-06-29T00:57:03Z")

</div>

@farmS1m good job !!

> [@farmS1m](#):
>
> initial Lua code did not work. Is it because of the dash in “Content-Type”?

From the Lua 5.3 Ref. Man.

- _Names_ (also called _identifiers_) in Lua can be any string of letters, digits, and underscores, not beginning with a digit and not being a reserved word.

---

<div class="post-metadata">

**Author:** ![system](https://forum.farmbot.org/user_avatar/forum.farmbot.org/system/32/4564_2.png) [@system](https://forum.farmbot.org/u/system)\
**Post date:** [July 29, 2024, 12:57am UTC](https://forum.farmbot.org/t/lua-http-request-with-content-type-application-x-www-form-urlencoded/9223/4 "2024-07-29T00:57:08Z")

</div>

Closed on July 29, 2024, 12:57am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
