Help

Re: POST Requests Not Working?

571 0
cancel
Showing results for 
Search instead for 
Did you mean: 
izzylys
4 - Data Explorer
4 - Data Explorer

Hello everone!

I’m trying to make POST requests and am stuck with IronPython but I am having some problems creating records. I’m able to create them using the requests module in Python3, but I’m not able to replicate this with WebRequest in IronPython. Here is the code:

def post_request(params):
    '''post data to Airtable'''
    HEADERS = 'Authorization: Bearer XXXXX'
    URI = 'https://api.airtable.com/v0/XXXXXXXXXX/Table%20XX'

    request = WebRequest.Create(URI)
    request.ContentType = 'application/json'
    request.Headers.Add(HEADERS)
    request.Method = 'POST'

    bytes = Encoding.ASCII.GetBytes(json.dumps(params))
    request.ContentLength = bytes.Length
    with request.GetRequestStream() as reqStream:
        reqStream.Write(bytes, 0, bytes.Length)

    response = request.GetResponse()
    result = StreamReader(response.GetResponseStream()).ReadToEnd()

    return result

The params are formatted as follows:

    params = {
        'fields': {
            'Room Type': 'Test Room',
            'Project': 'Test Proj',
            'Room Area (m2)': 15,
        }
    }

This function has worked to post to other APIs, so I think there’s something wrong with the way the params are being passed. The error is thrown at response = request.GetResponse()

SystemError: The remote server returned an error: (422) Unprocessable Entity.

Has anyone had any experience with this? I’m not super comfortable with IronPython and am not sure what else I can try. Any help would be greatly appreciated!!

Thank you :sparkles:

1 Reply 1
IT_BeeTee
6 - Interface Innovator
6 - Interface Innovator

Hi there,

Don’t know if you have figured it out or not but I decide to reply anyway in case others need it later on.

I have come across similar error code with a PATCH and it turned out one of my fields were not updatable (i.e. I tried to update a lookup field).

I suggest you to use an app like Postman that simulates REST calls to see what Airtable returns https://www.getpostman.com/.

Last but not least, for a POST, I do have more header’s properties (it’s in Java fyi):

securedConnection.setRequestProperty("Authorization", "Bearer xxxx");
securedConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
securedConnection.setRequestProperty("Accept", "application/json");

Hope it helped,

Alex