0

I'm trying to send this json array to an API using request.post():

{
  "insert": [
    {
      "data": "48bPRVkgvHwjG2VUTkPLaGazynZ6RxETuNGsYZNBrtb7ZkAUqY1NE2iGqoLd8EFsvhbDGW8gNb96Jce8fg2aiY8A5mbd8zf",
      "tag": "0x1001"
    }
  ]
}

I was told the json post has to be in an array by one of the devs at the site I'm posting to. I also pored over these two examples in the API docs.1(edit emojiId section),2(adding an emoji id record)

What I tried so far is posting this dictionary:

{'insert': [{'data': '48bPRVkgvHwjG2VUTkPLaGazynZ6RxETuNGsYZNBrtb7ZkAUqY1NE2iGqoLd8EFsvhbDGW8gNb96Jce8fg2aiY8A5mbd8zf', 'tag': '0x1001'}]}

Using both of these post requests:

requests.post(base_url + '/emoji_id/', json=dict_data, headers=headers)
requests.post(base_url + '/emoji_id/', data=dict_data, headers=headers)

I then converted the dictionary to a string using

dict_data = json.dumps(dict_data, skipkeys=True, separators=(',', ':'))

and again sent it using both json and data.

Each returned a 405 error.

The next thing I tried is using the example this stack post to send a nested json. Here's my code for that attempt:

params = [{'data':'48bPRVkgvHwjG2VUTkPLaGazynZ6RxETuNGsYZNBrtb7ZkAUqY1NE2iGqoLd8EFsvhbDGW8gNb96Jce8fg2aiY8A5mbd8zf','tag':'0x1001'}]
# Tried above both with [ ] on outside making it a list and without
payload = {'insert': json.dumps(params, skipkeys=True, separators=(',', ':'))}
requests.post(base_url + '/emoji_id/', json=payload, headers=headers)

Again, I used both json=payload and data=payload and again I tried it both as a dictionary and converting to a string using the same json.dumps method as above. Same as before each returned a 405 response.

My question is, am I using request.post() correctly in at least one of these attempts? If so, I must be missing something about what the API expects and can start down that rabbit hole.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
z0rg0n
  • 1
  • 1
  • What does the response body look like? – BrokenBenchmark Mar 03 '22 at 03:29
  • Welcome to SO. Are you certain you're using the right method? Which part of the api are you using? From my brief look, the 'edit emoji' uses the ```patch``` method. – ewokx Mar 03 '22 at 03:31
  • @BrokenBenchmark Each responce.content was: b'' Which kind of reinforces that I'm just doing something wrong. @ewong Ty! I'm pretty new to coding and entirely new to working with APIs and JSON. I didn't know there were different methods of sending these requests before just now. I've only used ```GET``` and ```POST``` so far. I'll do some research into ```post``` now. I bet that's what I'm missing. Thanks again! – z0rg0n Mar 03 '22 at 03:54

1 Answers1

0

@ewong nailed it. I didn't realize I had to use the patch method instead of the post method.

So my issue was fixed using:

requests.patch(base_url + '/emoji_id/', json=dict_data, headers=headers)

instead of request.post. The difference between the two still seems subtle to me at this point but the example given in the API docs clearly show a patch method being used and that's what worked.

z0rg0n
  • 1
  • 1