AstronomyAPI/Samples

Example Python code for moon image from demo console fails with Error 422

Closed this issue · 2 comments

Hi - Yet more help please

I can get moon image displayed successfully in the Demo Page, so my app id and secret are now ok.

I cut and paste the example python code (below) directly from the Demo Page Console, but get a 422 error when I run it in python 3.9.2

I have copied the python code from the console and the error it produces below

CODE
import http.client
conn = http.client.HTTPSConnection("api.astronomyapi.com")
payload = "{"style":{"moonStyle":"default","backgroundStyle":"stars","backgroundColor":"#000000","headingColor":"#ffffff","textColor":"#ffffff"},"observer":{"latitude":53,"longitude":-1.5,"date":"2023-10-24"},"view":{"type":"portrait-simple","parameters":{}}}"
headers = { 'Authorization': "Basic ZTc2YmQ1YzktMjU3NC00MjRlLWI1MzktZmY3OWQ2MWE0Njk5OjVmMmQ2YjczNDRjZTdjMmJkZDI4Yjc5MzZkNGRiYWQ2MjgyZDYyMjljZmExMTIyNDVhZDhmMjFjZjRkZDBmODBmNDY2MmM2OGUzYWY5MTNkODUzNjU1NGQ5YjExNGFiYTU3NzJkNmYzNDg4ZjRlMmQ5ZDk4MTIzNjNlNGVkMjE3M2E3NDIxODliMGFiMmRhYmMwZDY2ZmU2OWI4NzkzYzg2NGM2MjUwYTBkOWQzODAyNWFmZjlkMjhmZGRhZjEwNjBmMTAzNDE4M2E4NWQ1ZGM2MTM4NzJhYzI3ZGZkNTU5" }
conn.request("POST", "/api/v2/studio/moon-phase", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

RESPONSE
{"statusCode":422,"errors":[{"path":[],"property":"instance","message":"requires property "observer"","schema":{"type":"object","properties":{"format":{"type":"string","enum":["svg","png"]},"style":{"type":"object","properties":{"moonStyle":{"type":"string","enum":["sketch","default","shaded"]},"backgroundStyle":{"type":"string","enum":["stars","solid"]},"backgroundColor":{"type":"string"},"headingColor":{"type":"string"},"textColor":{"type":"string"}}},"observer":{"type":"object","properties":{"latitude":{"type":"number","minimum":-90,"maximum":90},"longitude":{"type":"number","minimum":-180,"maximum":180},"date":{"anyOf":[{"type":"string","format":"date"},{"type":"string","format":"date-time"}]}},"required":["latitude","longitude","date"]},"view":{"type":"object","properties":{"type":{"type":"string","enum":["portrait-simple","landscape-simple"]},"orientation":{"type":"string","enum":["north-up","south-up"]}},"required":["type"]}},"required":["observer","view","style"]},"instance":{},"name":"required","argument":"observer","stack":"instance requires property "observer""},{"path":[],"property":"instance","message":"requires property "view"","schema":{"type":"object","properties":{"format":{"type":"string","enum":["svg","png"]},"style":{"type":"object","properties":{"moonStyle":{"type":"string","enum":["sketch","default","shaded"]},"backgroundStyle":{"type":"string","enum":["stars","solid"]},"backgroundColor":{"type":"string"},"headingColor":{"type":"string"},"textColor":{"type":"string"}}},"observer":{"type":"object","properties":{"latitude":{"type":"number","minimum":-90,"maximum":90},"longitude":{"type":"number","minimum":-180,"maximum":180},"date":{"anyOf":[{"type":"string","format":"date"},{"type":"string","format":"date-time"}]}},"required":["latitude","longitude","date"]},"view":{"type":"object","properties":{"type":{"type":"string","enum":["portrait-simple","landscape-simple"]},"orientation":{"type":"string","enum":["north-up","south-up"]}},"required":["type"]}},"required":["observer","view","style"]},"instance":{},"name":"required","argument":"view","stack":"instance requires property "view""},{"path":[],"property":"instance","message":"requires property "style"","schema":{"type":"object","properties":{"format":{"type":"string","enum":["svg","png"]},"style":{"type":"object","properties":{"moonStyle":{"type":"string","enum":["sketch","default","shaded"]},"backgroundStyle":{"type":"string","enum":["stars","solid"]},"backgroundColor":{"type":"string"},"headingColor":{"type":"string"},"textColor":{"type":"string"}}},"observer":{"type":"object","properties":{"latitude":{"type":"number","minimum":-90,"maximum":90},"longitude":{"type":"number","minimum":-180,"maximum":180},"date":{"anyOf":[{"type":"string","format":"date"},{"type":"string","format":"date-time"}]}},"required":["latitude","longitude","date"]},"view":{"type":"object","properties":{"type":{"type":"string","enum":["portrait-simple","landscape-simple"]},"orientation":{"type":"string","enum":["north-up","south-up"]}},"required":["type"]}},"required":["observer","view","style"]},"instance":{},"name":"required","argument":"style","stack":"instance requires property "style""}]}

After much googling, I have now resolved this issue.
The sample code needs an additional item adding into the Header before the Authorization element.

'Content-Type': 'application/json'

Reagrds
John D

Glad you resolved it!. Could be an improvement.