Errors when using the API

328 views
Skip to first unread message

e...@selfiecause.com

unread,
Mar 28, 2018, 11:05:14 AM3/28/18
to Back4App
Hi, I've been trying to figure out how to post files to my database. My end goal is images, but right now I'm struggling with just text files. My code is this
import json,http.client as httplib
connection = httplib.HTTPSConnection('parseapi.back4app.com', 443)
connection
.connect()
connection
.request('POST', '/parse/files/hello.txt', 'Hello, World!', {
       
"X-Parse-Application-Id": app_id,
       
"X-Parse-REST-API-Key": "${" + api_key + "}",
       
"Content-Type": "text/plain"
     })
result
= json.loads(connection.getresponse().read())
print(result)
And the results are, without fail:

{'error': 'Unexpected token H in JSON at position 0'}

I don't understand where I'm going wrong.
Thanks

e...@selfiecause.com

unread,
Mar 28, 2018, 5:13:57 PM3/28/18
to Back4App
When I leave the part of the code that currently says "Hello, World!" blank, just empty quotes, I get a different result: {'error': 'unauthorized'}

nat...@back4app.com

unread,
Mar 29, 2018, 6:49:42 AM3/29/18
to Back4App
Hi,

thanks for reaching out!
Could you please apply test to us? More specifically, at the code that you sent:

1. Delete the '{}' from Rest Key.
2. The address used to connect, change too: https://parseapi.back4app.com/ 

import json,http.client as httplib
connection = httplib.HTTPSConnection('https://parseapi.back4app.com/', 443)

connection
.connect()
connection
.request('POST', '/parse/files/hello.txt', 'Hello, World!', {
       
"X-Parse-Application-Id": app_id,

       
"X-Parse-REST-API-Key": " + api_key + ",

       
"Content-Type": "text/plain"
     })
result
= json.loads(connection.getresponse().read())
print(result)


Feel free to contact us anytime you need!


Regards,
Natália.

e...@selfiecause.com

unread,
Mar 29, 2018, 1:23:00 PM3/29/18
to Back4App
When doing that I get this traceback error: 
Traceback (most recent call last):
  File "/Users/enerney/PycharmProjects/SelfieCause/Interaction_With_B4A/send_to_db.py", line 7, in <module>
    connection.connect()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1392, in connect
    super().connect()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 936, in connect
    (self.host,self.port), self.timeout, self.source_address)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socket.py", line 704, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socket.py", line 743, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
I tried keeping the same address but changing the key I get this:
{'error': 'Unexpected token H'}

Thanks for your response, what should I try next?


On Wednesday, March 28, 2018 at 11:05:14 AM UTC-4, e...@selfiecause.com wrote:

nat...@back4app.com

unread,
Apr 2, 2018, 11:15:19 AM4/2/18
to Back4App
Hi!

I tried the code below and it worked, I just need to insert a "decode('utf-8')" and delete the "/parse/", please take a look at the guide below:

#!/usr/bin/python3

import json,http.client as httplib

connection = httplib.HTTPSConnection('parseapi.back4app.com', 443)

connection.connect()

connection.request('POST', '/files/files.txt', 'Hello, World!', {
       "X-Parse-Application-Id": "appID",
       "X-Parse-REST-API-Key": "restKey",
       "Content-Type": "text/plain"
     })

result = json.loads(connection.getresponse().read().decode('utf-8'))

print(result)

If you have any further information, feel free to contact us :)

Regards,
Natália.
Reply all
Reply to author
Forward
0 new messages