Unable to get data from a Website API access

Hi,

I am trying to get json data from API from another website but I am unable to get a correct response from the website, I am suspecting that my response code being sent to the website is not correct. I would request anyone to help me find a way to make the correct request.

Code:

    def getOrderShipmentDetails(self):
        url = "https://shipway.in/api/getOrderShipmentDetails"
        controller = frappe.get_doc("Shipway Settings")
        request = {
            "username": controller.username,
            "password": controller.license_key,
            "order_id": self.document_name
            }
        #self.test_data = str(controller.username)
        frappe.msgprint(json.dumps(request))
        response = make_get_request(url, auth=None, headers = None, data = request)
        frappe.msgprint(json.dumps(response))

The response I am getting the from the website is as below:

{"status": "failed", "msg": "Invalid inputs ,Please send username , password and (order id or awbno)"}

The API documentation of the website is also enclosed but since this is my first time developing a connector I am presuming I am doing something silly

Any help on this would be greatly appreciated.

@adityaduggal,

getOrderShipmentDetails api requires POST method and you are using the GET method to login

try to use the make_post_request method

Well I think I pasted the wrong code but I have already tried with post request and I am still getting the same response.
Is there a way I could check what erpnext is posting to the website since the response from the website is that I am not adding my user credentials which I am unable to debug as to what is make_post_request output is:

response = make_post_request(url, auth=None, headers = None, data = request)

pass username and password as tuple to auth param to make_post_request.

 make_post_request(url=url, auth=(username, password), headers=None,  data={ "order_id": self.document_name})

Just tried with the same but same response here is the code:

from __future__ import unicode_literals
import frappe
import json
from frappe.model.document import Document
from frappe.utils import get_url, call_hook_method, cint
from frappe.integrations.utils import make_get_request, make_post_request, create_request_log

class CarrierTracking(Document):
    def getOrderShipmentDetails(self):
        url = "https://shipway.in/api/getOrderShipmentDetails"
        controller = frappe.get_doc("Shipway Settings")
        request = {
            "username": controller.username,
            "password": controller.license_key,
            "order_id": self.document_name
            }
        #self.test_data = str(controller.username)
        frappe.msgprint(json.dumps(request))
        frappe.msgprint(controller.username)
        frappe.msgprint(controller.license_key)
        response = make_post_request(url=url, auth=(controller.username, controller.license_key), headers = None, data = {"order_id": self.document_name})
        frappe.msgprint(json.dumps(response))

The response is as below:

I am kind of foxed as to how this thing would work.
Is there a way I could check it via a BROWSER if possible?

Please try with.

import json
request = {
            "username": controller.username,
            "password": controller.license_key,
            "order_id": self.document_name
            }

response = make_post_request(url=url, auth=None, headers=None, data=json.dumps(request))

To test via browser you can use chrome’s Postman

1 Like

So now we have moved forward but I think there is an error in utils.py

Traceback (most recent call last):
  File "/home/aditya/frappe-bench/apps/frappe/frappe/app.py", line 56, in application
    response = frappe.handler.handle()
  File "/home/aditya/frappe-bench/apps/frappe/frappe/handler.py", line 21, in handle
    data = execute_cmd(cmd)
  File "/home/aditya/frappe-bench/apps/frappe/frappe/handler.py", line 52, in execute_cmd
    return frappe.call(method, **frappe.form_dict)
  File "/home/aditya/frappe-bench/apps/frappe/frappe/__init__.py", line 920, in call
    return fn(*args, **newargs)
  File "/home/aditya/frappe-bench/apps/frappe/frappe/handler.py", line 80, in runserverobj
    frappe.desk.form.run_method.runserverobj(method, docs=docs, dt=dt, dn=dn, arg=arg, args=args)
  File "/home/aditya/frappe-bench/apps/frappe/frappe/desk/form/run_method.py", line 35, in runserverobj
    r = doc.run_method(method)
  File "/home/aditya/frappe-bench/apps/frappe/frappe/model/document.py", line 666, in run_method
    out = Document.hook(fn)(self, *args, **kwargs)
  File "/home/aditya/frappe-bench/apps/frappe/frappe/model/document.py", line 887, in composer
    return composed(self, method, *args, **kwargs)
  File "/home/aditya/frappe-bench/apps/frappe/frappe/model/document.py", line 870, in runner
    add_to_return_value(self, fn(self, *args, **kwargs))
  File "/home/aditya/frappe-bench/apps/frappe/frappe/model/document.py", line 660, in <lambda>
    fn = lambda self, *args, **kwargs: getattr(self, method)(*args, **kwargs)
  File "/home/aditya/frappe-bench/apps/rigpl_erpnext/rigpl_erpnext/rigpl_erpnext/doctype/carrier_tracking/carrier_tracking.py", line 25, in getOrderShipmentDetails
    response = make_post_request(url=url, auth = None, headers = None, data = json.dumps(request))
  File "/home/aditya/frappe-bench/apps/frappe/frappe/integrations/utils.py", line 48, in make_post_request
    raise exc
TypeError: unsupported operand type(s) for -=: 'Retry' and 'int'

Please note that there might be an error in the retry code of utils but

response = make_post_request(url=url, auth=None, headers=None, data=json.dumps(request))

Worked like a charm.