0

I have the following base URL

https://ACCOUNT_ID.suitetalk.api.netsuite.com/services/rest/record/v1/salesOrder

Works fine to give me all the sales orders. However, when I add the following query params

?q=createdDate AFTER "01/01/2022"

It says invalid URL, no schema supplied. Works fine in postman though.

I tried having that query param exactly in the url like so;

https://ACCOUNT_ID.suitetalk.api.netsuite.com/services/rest/record/v1/salesOrder?q=createdDate AFTER "01/01/2022"

Also tried having it as a params in requests, but this gives me an auth token error.

headers = {token_info,private_keys}
params = {'createdDate': 'AFTER "01/01/2022"'}
payload = {}

requests.request(HTTP_METHOD, BASE_URL, params=params, headers=headers, data=payload)

Wondering what else I could try.

Christian Baumann
  • 3,188
  • 3
  • 20
  • 37
itsPav
  • 516
  • 2
  • 14
  • 25

1 Answers1

1

The trouble here is the special characters in the query parameter. They need to be properly encoded. Here is a discussion on how to encode space characters: URL encoding the space character: + or %20? The / characters need to be encoded as %2F sequences and " as %22.

I have done a little experiment with the requests library and it seems that it expects url arguments to be properly encoded already:

>>> BASE_URL = 'http://localhost:8000?q=createdDate AFTER "01/01/2022"'
>>> params = None
>>> requests.request(HTTP_METHOD, BASE_URL, params=params, headers=headers, data=payload)
127.0.0.1 - - [22/Apr/2022 07:14:25] "GET /?q=createdDate%20AFTER%20%2201/01/2022%22 HTTP/1.1" 200 100

As you can see, for some reason, it encodes the space character as a %20 sequence, whilst you need it to be encoded as a + character, and the / character is not encoded at all.

In order to fix that you would have to provide it the following BASE_URL:

>>> BASE_URL = 'https://ACCOUNT_ID.suitetalk.api.netsuite.com/services/rest/record/v1/salesOrder?q=createdDate+AFTER+%2201%2F01%2F2022%22'
>>> requests.request(HTTP_METHOD, BASE_URL, params=params, headers=headers, data=payload)
127.0.0.1 - - [22/Apr/2022 07:40:52] "GET /?q=createdDate+AFTER+%2201%2F01%2F2022%22 HTTP/1.1" 200 100

There are some libraries available that can properly encode the URL for you, but as you have already found out, requests can do that as well (and Postman too). All you need is to pass the query parameters as a params argument.

But there is a little mistake in your code snippet. You pass a query parameter with a key createdDate and a value AFTER "01/01/2022":

>>> BASE_URL = 'http://localhost:8000'
>>> params = {'createdDate': 'AFTER "01/01/2022"'}
>>> requests.request(HTTP_METHOD, BASE_URL, params=params, headers=headers, data=payload)
127.0.0.1 - - [22/Apr/2022 07:48:27] "GET /?createdDate=AFTER+%2201%2F01%2F2022%22 HTTP/1.1" 200 100

Notice the missing q= there. You probably need to pass a query parameter with a key q and a value createdDate AFTER "01/01/2022":

>>> BASE_URL = 'https://ACCOUNT_ID.suitetalk.api.netsuite.com/services/rest/record/v1/salesOrder'
>>> params = {'q': 'createdDate AFTER "01/01/2022"'}
127.0.0.1 - - [22/Apr/2022 07:50:20] "GET /?q=createdDate+AFTER+%2201%2F01%2F2022%22 HTTP/1.1" 200 100
radekholy24
  • 418
  • 2
  • 12
  • So I encode the base URL when I make the auth header, and then tried the params with 'q' as well. In this case it says token_rejected. Now I'm wondering if I have to pass the whole url to the auth header logic. – itsPav Apr 22 '22 at 15:51
  • This is what my url ends up being; `https://ACCOUNT_ID.suitetalk.api.netsuite.com/services/rest/record/v1/salesOrder?q=createdDate+AFTER+%2201%2F01%2F2022%22` – itsPav Apr 22 '22 at 16:04
  • There might be multiple reasons for token rejection. Maybe it has expired. Try to generate a new one. – radekholy24 Apr 22 '22 at 20:26
  • Says I got an InvalidSignature. Wondering if I need to pass the query params as well when generating my OauthHeader. Right now it just has the usual params; nonce, keys, timestamp, etc. – itsPav Apr 22 '22 at 21:29
  • 1
    I recommend you to use the Requests-Oauthlib library that can sign the requests for you: https://requests-oauthlib.readthedocs.io/en/latest/oauth1_workflow.html – radekholy24 Apr 23 '22 at 04:37
  • That worked. Now I'm trying to find an async requests lib for oauth1. – itsPav Apr 29 '22 at 20:47
  • Is there anywhere that specifies the filters than can be used for a given field? I'm attemtping something similar using email in customer endpoint and am only able to use the "IS" keyword – ben890 Jun 07 '22 at 14:52