0

I'm working on a python script that will communicate with the API of a CRM system I'm deploying right now. I can get data from the CRM server, but I can't seem to add (write) a new entry. I suspect I'm doing something silly because I'm fairly new to Python and programming in general, can someone point me in the right direction? The server does not reject the data, but it acts as if I was requesting data from /api/v1.0/payments as opposed to posting new data.

from urllib.request import Request, urlopen

headers = {
  'Content-Type': 'application/json',
  'X-Auth-App-Key': '[API key]'
}

values = b"""
  {
"clientId": 104,
"method": 3,
"checkNumber": "",
"createdDate": "2016-09-12T00:00:00+0000",
"amount": 40,
"note": "",
  }
"""

request = Request('http://[SERVER_URL]/api/v1.0/payments', data=values, headers=headers)

response_body = urlopen(request).read()
print(response_body)

I'm working based on example code from the API documentation here: http://docs.ucrm.apiary.io/#reference/payments/payments/post

Am I using urlopen correctly at the bottom?

Detoxica
  • 5
  • 4
  • You don't seem to modify any data, just request & print some information. What exactly would you like to get / do ? What error messages (if any) do you get ? –  Mar 29 '17 at 12:31

1 Answers1

0

This question/answer may be your issue. Basically your POST request is being redirected to /api/v1.0/payments/ (note the trailing slash), when that happens your POST is redirected to a GET request, which is why the server is responding as if you were trying to retrieve all of the payments info.

Other things to note are your json data is actually invalid as it contains a trailing , after the 'note' value, so that may be an issue issue too. I think you may also be missing the Content-Length header in your headers. I'd recommend using the json module to create your json data:

values = json.dumps({
    "clientId": 104,
     "method": 3,
     "checkNumber": "",
     "createdDate": "2016-09-12T00:00:00+0000",
     "amount": 40,
     "note": ""
})

headers = {
  'Content-Type': 'application/json',
  'Content-Length': len(values),
  'X-Auth-App-Key': '[API key]'
}

request = Request('http://[SERVER_URL]/api/v1.0/payments/', data=values, headers=headers)
Community
  • 1
  • 1
mshildt
  • 8,782
  • 3
  • 34
  • 41
  • Thanks for helping out. The server isn't actually rejecting the data, it's sending the same response as if I was trying to retrieve information from /api/v1.0/payments (dumps all existing payment information on the server in json format). I'm thinking this might be an issue with the example code that I'm working with, but I want to sort out any issues with my own code before I go to the platform's forums. Is using the json module just best practice, or is there an issue with the format of my code? – Detoxica Mar 29 '17 at 14:31
  • I would consider it best practice, but I'm no authority on the matter. It's certainly more versatile as you can easily use python expressions for the values of the object's keys. Only issue with the format of your code is the trailing comma in `"note": "",`. – mshildt Mar 29 '17 at 14:43
  • The extra info you provided in your first comment should be in the main question. It really helps describe the actual problem you are having. See my updates to my answer... – mshildt Mar 29 '17 at 14:51
  • Thanks, I've edited the question to reflect that. I've tried using payments/ but I'm getting an error: "raise HTTPError(req.full_url, code, msg, hdrs, fp)" and "HTTPError: Not Found" – Detoxica Mar 29 '17 at 15:10